add records between pattern (awk)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting add records between pattern (awk)
# 1  
Old 05-06-2008
add records between pattern (awk)

2008 Tue Apr 22 00:11:50
1.0
2.1
4.0
2008 Tue Apr 22 00:26:51
4.0
5.0
6.2

With the above as the input file, I would like to output:
2008 Tue Apr 22 00:11:50
7.1
2008 Tue Apr 22 00:26:51
15.2

In other words add the (numeric) records between the records that start with 2008 (all the dates start with 2008 in this data).

Tried a couple of variations on:
Code:
/^2008/ { total = 0; print$0; getline; while ($1 != "2008") {total += $1; getline}; print total }

Can't get it right - when the while loop condition becomes false processing resumes after the date which made it so, and hence that date it skipped and it proceeds in this way to only print every second date with associated total.
That's not very clear, sorry about that. Any help appreciated.
# 2  
Old 05-06-2008
Code:
 awk '{ if(/^2008/) { print total; total=0; print $0}
                     else { total+=$0}
        ' filename

# 3  
Old 05-06-2008
This should give you the desired output:

Code:
awk '
NF==1{s+=$0;next}
s{print s;s=0}1
END{print s}' file

Use nawk or /usr/xpg4/bin/awk on Solaris.


Regards
# 4  
Old 05-08-2008
Great, many thanks guys, that really helps me out.
# 5  
Old 05-08-2008
Hi Franklin,

Can you please eloborate on the third line of your script.

s{print s;s=0}1

I cant get the significance of 1 at the end.

Thanks in advance
# 6  
Old 05-08-2008
awk evaluate the 1 as a true and by default it prints the whole line, it's similar to {print $0}.

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed and awk to find pattern and add priffix

Original File Server1|poweredOn|268401| Server1/Server1.vmx|Red Hat Enterprise Linux 5 (64-bit) Need Output Server1|poweredOn|DR|T1|268401| Server1/Server1.vmx|Red Hat Enterprise Linux 5 (64-bit) Conduction to check find the string "SFCHT1" and "SR" and add prefix has... (4 Replies)
Discussion started by: ranjancom2000
4 Replies

2. Shell Programming and Scripting

awk to add text to matching pattern in field

In the awk I am trying to add :p.=? to the end of each $9 that matches the pattern NM_. The below executes andis close but I can not seem to figure out why the :p.=? repeats in the split as in the green in the current output. I have added comments as well. Thank you :). file ... (4 Replies)
Discussion started by: cmccabe
4 Replies

3. Shell Programming and Scripting

[awk] add column between range pattern

Hi all, I have table like this A1 1 2 1 3 2 4 A2 3 1 1 2 1 1 A3 1 1 2 0 3 2 ..... An And i want to add column to my table and the table will become like this : (3 Replies)
Discussion started by: psychop13
3 Replies

4. Shell Programming and Scripting

awk pattern matching name in records

Hi, I'm very new to these forums. I was wondering if someone could help an AWK beginner with a pattern matching an actor to his appearance in movies, which would be stored as records. Let's say we have a database of 4 movies (each movie a record with name, studio + year, and actor fields with... (2 Replies)
Discussion started by: Jill Ceke
2 Replies

5. Shell Programming and Scripting

Searching for a pattern and extracting records related to that pattern

Hi there, Looking forward to your advice for the below: I have a file which contains 2 paragraphs related to a particular pattern. I have to search for those paragraphs from a log file and then print a particular line from those paragraphs. Sample: I have one file with the fixed... (3 Replies)
Discussion started by: danish0909
3 Replies

6. Shell Programming and Scripting

How to add columns based on a pattern using awk?

Hi, I have a file with more than 1000 lines with ~14 columns. I need to find all the lines with matching value in column 14 and then add column 6 in all the lines before printing them out.. e.g if this is the input file: abc test input 10 for process 2345 abc test input 15 for process 2348... (1 Reply)
Discussion started by: xkdasari
1 Replies

7. Shell Programming and Scripting

[Solved] Find duplicate and add pattern in sed/awk

<Update> I have the solution: sed 's/\{3\}/&;&;---;4/' The thread can be marked as solved! </Update> Hi There, I'm working on a script processing some data from a website into cvs format. There is only one final problem left I can't find a solution. I've processed my file... (0 Replies)
Discussion started by: lolworlds
0 Replies

8. Shell Programming and Scripting

awk to find pattern and add lines

My file goes like this: SID_LIST_HOSTNAME_LISTENER_3 = (SID_LIST = (SID_DESC = (SID_NAME = ORA0008) (ORACLE_HOME = /opt/oracle/product/ORA0008) (ENVS = "LD_LIBRARY_PATH=/opt/oracle/product/ORA0008/lib") ) (SID_DESC = (SID_NAME = ORA0007) ... (4 Replies)
Discussion started by: jpsingh
4 Replies

9. Shell Programming and Scripting

AWK, print no of records after pattern match.

Hi ALL :). i have a file, cat 3 + dog 5 + rat 6 - i want to print no of record having pattern "+". thanks in advance :confused:. (2 Replies)
Discussion started by: admax
2 Replies

10. Shell Programming and Scripting

print records before and after the pattern

Hi All, I want to write a sh shell script to read input report and search for a specefic pattern in file and print 3 record above and 2 record below the line in which pattern found. My file contains different- different pattern repeatedly, same pattern set of records should go into one file... (9 Replies)
Discussion started by: singhald
9 Replies
Login or Register to Ask a Question