[Solved] AWK to parse adjacent matching lines

 
Thread Tools Search this Thread
Homework and Emergencies Emergency UNIX and Linux Support [Solved] AWK to parse adjacent matching lines
# 1  
Old 09-27-2011
[Solved] AWK to parse adjacent matching lines

Hi,

I have an input file like

Code:
F :   0.1 : 0.002
P :  0.3  : 0.004
P : 0.5 : 0.008
P : 0.1 : 0.005
L : 0.05 : 0.02
P: 0.1 : 0.006
P : 0.01 : 0.08
F : 0.02 : 0.08


Expected output:

Code:
F :   0.1 : 0.002
P :  0.3  : 0.017     (Here it is addition of  0.004+ 0.008 + 0.005)
L : 0.05 : 0.02
P: 0.1 : 0.086   ( (Here it is addition of  0.006 + 0.08 )
F : 0.02 : 0.08


That is here ^P rows third column get added and made as single column.

In awk how to get it.

Thanks
Vasanth

Smilie
# 2  
Old 09-27-2011
Code:
awk -F' *: *' '
NR == 1    { prev = $1; a = $2 }
$1 == prev { v[$1] += $3 }
$1 != prev { print prev " : " a " : " v[prev]; v[prev] = ""
  v[$1] = $3; a = $2
}
{ prev = $1 }
END { print prev " : " a " : " v[prev] }
' INPUTFILE

# 3  
Old 09-27-2011
Hi,

Your code worked..


First because of improper spaces, i got into issue.

To get rid of spaces

sed "s/[' ']*//g" infile > mid_file

then your code as check_scr.awk
Code:
NR == 1    { prev = $1; a = $2 } 
$1 == prev { v[$1] += $3 } 
$1 != prev { print prev " : " a " : " v[prev]; v[prev] = ""   
v[$1] = $3; a = $2 
} 
{ prev = $1 } 
END { print prev " : " a " : " v[prev] }

then from prompt window
Code:
awk -F: -f chek_scr.awk mid_file


Thanks
VasanthSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to parse multiple lines

What is the correct syntax to have the awk parse the next line as well? The next in bold is where I think it should go, but I wanted to ask the experts since I am a beginner. The file to be parsed is attached as well. Thank you :). awk 'NR==2 {split($2,a,"");b=substr(a,1,length(a-1));print... (6 Replies)
Discussion started by: cmccabe
6 Replies

2. Shell Programming and Scripting

awk - matching on 2 columns for differents lines

Given this file (I separated them in block to make my explanation clearer): 92157768877;Sof_deme_Fort_Email_am_%yyyy%%mm%%dd%;EMAIL;20/02/2015;1;0;0 92157768877;Sof_trav_Fort_Email_am_%yyyy%%mm%%dd%;EMAIL;20/02/2015;1;0;0 91231838895;Sof_deme_faible_Email_am;EMAIL;26/01/2015;1 0;0... (1 Reply)
Discussion started by: Andy_K
1 Replies

3. Shell Programming and Scripting

[solved] Awk/shell question to parse hour minute from text

Hi, I have a quick question on parsing the hour/minute and value from a text file and remove the seconds portion. For example in the below text file: 20:26:01 95.83 20:27:01 96.06 20:28:01 95.99 20:29:01 7.11 20:30:01 5.16 20:31:01 8.27 20:32:02 9.79 20:33:01 11.27 20:34:01 7.83... (2 Replies)
Discussion started by: satishrao
2 Replies

4. Shell Programming and Scripting

[Solved] awk to remove lines

Hi, I have a file with contents. file1: <2013 tttaaa abc123 <2013 gggdddd <2013 sssssss <2013 eeeee I need to remove the lines which do not have the word "tttaaa" can some one help ? (7 Replies)
Discussion started by: giri_luck
7 Replies

5. UNIX for Dummies Questions & Answers

[SOLVED] awk: matching degenerate patterns

Hi Folks, I have two arrays a: aaa bbb ccc ddd ddd aaa bbb ccc ddd ccc aaa bbb b: aaa bbb ccc aaa ccc bbb bbb aaa ccc ccc bbb aaa I want to compare row by row a(c1:c4) to b(c1:c3). If elements of 'b' match... (5 Replies)
Discussion started by: heecha
5 Replies

6. Shell Programming and Scripting

Help With AWK Matching and Re-printing Lines

Hi All, I'm looking to use AWK to pattern match lines in XML file - Example patten for below sample would be /^<apple>/ The sample I wrote out is very basic compared to what I am actually working with but it will get me started I would like to keep the matched line(s) unchanged but have them... (4 Replies)
Discussion started by: rhoderidge
4 Replies

7. Shell Programming and Scripting

[Solved] awk calculating between lines

Hey guys, maybe you can help me with this... I want to read input.dat line by line, while doing a simple calculation between the second column value of the current line and the second column value of the next line (like a difference). input is something like this: 0 3.945757 1 ... (1 Reply)
Discussion started by: origamisven
1 Replies

8. Shell Programming and Scripting

Merging Adjacent Lines Using Gawk

Hi all, I have a text file consisting of 4 columns. What I am trying to do is see whether column 2 repeats multiple times, and collapse those repeats into one row. For example, here is a snippet of the file I am trying to analyze: 1 Gamble_Win 14.282 0.502 1 Sure_Thing 14.858 0.174 1... (4 Replies)
Discussion started by: Jahn
4 Replies

9. Shell Programming and Scripting

How to subtract the adjacent lines from a single column?

Hi All, I have a file with only one column and i need to subtract the adjacent lines of the same column and print it in the same column. For Example: (Input) Col1 5 10 12 6 9 12 5 . . . .output should be like this: (12 Replies)
Discussion started by: Fredrick
12 Replies

10. UNIX for Dummies Questions & Answers

print adjacent lines

how do you print the lines before and after the line you are interested in? Example: Line to be printed: line 344 Output: line 343 line 344 line 345 Thanks (1 Reply)
Discussion started by: apalex
1 Replies
Login or Register to Ask a Question