How can I add 4 lines together and then read the next 4 lines?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How can I add 4 lines together and then read the next 4 lines?
# 8  
Old 11-25-2008
I was asking for the real input files, not their grepped versions Smilie
Could you post samples from both input files (obfuscating sensitive data, if needed)?
# 9  
Old 11-25-2008
Sorry I misunderstood Smilie


081125_08:38 14315 2.39 SOM.exe

OPERATIONAL CONTEXT 1
AO Total = 8867
AO Outstanding = 42
AO Acknowledged = 0
AO Terminated = 8825
AO Not Handled = 42
AO Handled = 0
AO Closed = 8825
AO Archived = 0

OPERATIONAL CONTEXT 2
AO Total = 2570
AO Outstanding = 2365
AO Acknowledged = 32
AO Terminated = 173
AO Not Handled = 2397
AO Handled = 0
AO Closed = 173
AO Archived = 0

OPERATIONAL CONTEXT 3
AO Total = 403
AO Outstanding = 312
AO Acknowledged = 2
AO Terminated = 89
AO Not Handled = 314
AO Handled = 0
AO Closed = 89
AO Archived = 0

OPERATIONAL CONTEXT 4
AO Total = 44258
AO Outstanding = 650
AO Acknowledged = 0
AO Terminated = 43608
AO Not Handled = 650
AO Handled = 0
AO Closed = 43608
AO Archived = 0


081125_09:40 14315 2.32 SOM.exe

OPERATIONAL CONTEXT 1
AO Total = 9002
AO Outstanding = 46
AO Acknowledged = 0
AO Terminated = 8956
AO Not Handled = 46
AO Handled = 0
AO Closed = 8956
AO Archived = 0

OPERATIONAL CONTEXT 2
AO Total = 2543
AO Outstanding = 2357
AO Acknowledged = 32
AO Terminated = 154
AO Not Handled = 2389
AO Handled = 0
AO Closed = 154
AO Archived = 0

OPERATIONAL CONTEXT 3
AO Total = 434
AO Outstanding = 312
AO Acknowledged = 2
AO Terminated = 120
AO Not Handled = 314
AO Handled = 0
AO Closed = 120
AO Archived = 0

OPERATIONAL CONTEXT 4
AO Total = 45241
AO Outstanding = 664
AO Acknowledged = 0
AO Terminated = 44577
AO Not Handled = 664
AO Handled = 0
AO Closed = 44577
AO Archived = 0


And I want to read the file and make an ouput like
081125_09:38 14315 2.32 SOM.exe Total {sum of the 4 Total}
081125_09:40 14315 2.32 SOM.exe Total {sum of the 4 Total }
# 10  
Old 11-25-2008
Is this a single file?
# 11  
Old 11-25-2008
yes it is
# 12  
Old 11-25-2008
Try this:
Code:
awk 'END {
  if (t) 
    print e, "Total:", t
    }
/exe$/ { 
  if (t) 
    print e, "Total:", t
  t = 0
  e = $0
  }
/Total/ { t += $NF }
' infile

# 13  
Old 11-25-2008
I am not entirely with you.
I have put this piece of code in a file and try to execute it. But nothing happens.
./test < infile
# 14  
Old 11-25-2008
Could you execute the script on the command line like this and post the result:

Code:
$ cat file
081125_08:38 14315 2.39 SOM.exe

OPERATIONAL CONTEXT 1
AO Total = 8867
AO Outstanding = 42
AO Acknowledged = 0
AO Terminated = 8825
AO Not Handled = 42
AO Handled = 0
AO Closed = 8825
AO Archived = 0

OPERATIONAL CONTEXT 2
AO Total = 2570
AO Outstanding = 2365
AO Acknowledged = 32
AO Terminated = 173
AO Not Handled = 2397
AO Handled = 0
AO Closed = 173
AO Archived = 0

OPERATIONAL CONTEXT 3
AO Total = 403
AO Outstanding = 312
AO Acknowledged = 2
AO Terminated = 89
AO Not Handled = 314
AO Handled = 0
AO Closed = 89
AO Archived = 0

OPERATIONAL CONTEXT 4
AO Total = 44258
AO Outstanding = 650
AO Acknowledged = 0
AO Terminated = 43608
AO Not Handled = 650
AO Handled = 0
AO Closed = 43608
AO Archived = 0


081125_09:40 14315 2.32 SOM.exe

OPERATIONAL CONTEXT 1
AO Total = 9002
AO Outstanding = 46
AO Acknowledged = 0
AO Terminated = 8956
AO Not Handled = 46
AO Handled = 0
AO Closed = 8956
AO Archived = 0

OPERATIONAL CONTEXT 2
AO Total = 2543
AO Outstanding = 2357
AO Acknowledged = 32
AO Terminated = 154
AO Not Handled = 2389
AO Handled = 0
AO Closed = 154
AO Archived = 0

OPERATIONAL CONTEXT 3
AO Total = 434
AO Outstanding = 312
AO Acknowledged = 2
AO Terminated = 120
AO Not Handled = 314
AO Handled = 0
AO Closed = 120
AO Archived = 0

OPERATIONAL CONTEXT 4
AO Total = 45241
AO Outstanding = 664
AO Acknowledged = 0
AO Terminated = 44577
AO Not Handled = 664
AO Handled = 0
AO Closed = 44577
AO Archived = 0

$ awk 'END {
  if (t) 
    print e, "Total:", t
}
/exe$/ { 
  if (t) 
    print e, "Total:", t
  t = 0
  e = $0
  }
/Total/ { t += $NF }
' file
081125_08:38 14315 2.39 SOM.exe Total: 56098
081125_09:40 14315 2.32 SOM.exe Total: 57220
$

If you want to put it in a script you can do something like this:

1. Prepare a script with the following content:

Code:
END {
  if (t) 
    print e, "Total:", t
  }
/exe$/ { 
  if (t) 
    print e, "Total:", t
  t = 0
  e = $0
  }
/Total/ { t += $NF }

2. Run the script on the command line:

Code:
awk -f <scriptname> <your_datafile>

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read few last lines of a file

Hi, I have a txt file in below format - START 1 2 3 4 END START 5 6 7 8 END START 9 10 END (8 Replies)
Discussion started by: bhupinder08
8 Replies

2. Shell Programming and Scripting

Extract some lines from one file and add those lines to current file

hi, i have two files. file1.sh echo "unix" echo "linux" file2.sh echo "unix linux forums" now the output i need is $./file2.sh unix linux forums (3 Replies)
Discussion started by: snreddy_gopu
3 Replies

3. Shell Programming and Scripting

search and replace, when found, delete multiple lines, add new set of lines?

hey guys, I tried searching but most 'search and replace' questions are related to one liners. Say I have a file to be replaced that has the following: $ cat testing.txt TESTING AAA BBB CCC DDD EEE FFF GGG HHH ENDTESTING This is the input file: (3 Replies)
Discussion started by: DeuceLee
3 Replies

4. UNIX for Dummies Questions & Answers

When reading a csv file, counter to read 20 lines and wait for minute then read next 20 till end

Hello All, i am a newbie and need some help when reading a csv file in a bourne shell script. I want to read 10 lines, then wait for a minute and then do a reading of another 10 lines and so on in the same way. I want to do this till the end of file. Any inputs are appreciated ... (3 Replies)
Discussion started by: victor.s
3 Replies

5. Shell Programming and Scripting

How to read different lines into column

Dear experts, Need your great help ! The input file is as follows: Name: test01 Name UID: C7:9D:79:52:2A:3F:DF:11:A8:64:00:60:16:36:04:02 Shareable: YES HBA/SP Pairs: HBA UID SP Name SPPort ------- ... (9 Replies)
Discussion started by: tojzz
9 Replies

6. Shell Programming and Scripting

Help!! Need script to read files and add values by lines...

Hi All, I really need your help. I am a begginner in shell script and I believe this is a very simple issue. I have in my directory, n-files, like 1.dhm, 2.dhm, 3.dhm. These files have 1 column with 1 value per line, like: 1.dhm ------ 10 20 30 40 50 2.dhm ------ 30 50 20 (3 Replies)
Discussion started by: dhuertas
3 Replies

7. UNIX for Dummies Questions & Answers

How to count lines - ignoring blank lines and commented lines

What is the command to count lines in a files, but ignore blank lines and commented lines? I have a file with 4 sections in it, and I want each section to be counted, not including the blank lines and comments... and then totalled at the end. Here is an example of what I would like my... (6 Replies)
Discussion started by: kthatch
6 Replies

8. Shell Programming and Scripting

read some lines from file!!!

any idea please!!! I want to pick up all lines of "state" and "desc" from x files: ... # state blah blah blah blah ... .. # desc blah blah blah .... Thx Andy (7 Replies)
Discussion started by: andy2000
7 Replies

9. UNIX for Dummies Questions & Answers

Read lines from file

i have a problem on my bourne shell script. I want to read line by line and then stop when the line does not have letters or is an empty string. But i encounter an error at "while ". The error nessage is "test.sh: test: unknown operator line". Can anyone help me on this thanks :) (2 Replies)
Discussion started by: sagolo
2 Replies

10. UNIX for Dummies Questions & Answers

how to read lines one by one from a file

I have one file in which some commands have written line line i have to read lines from this file(file name passed as avariable) and then i have to execute these commands.. how can i do it? (5 Replies)
Discussion started by: bihani4u
5 Replies
Login or Register to Ask a Question