awk is limited to only one line operations?

1

Like in the subject. Let say I have a file which contains:

*aaa A B C

D E F 55

*ccc A B C

R E T 33

and I would know if it's possibe to make a statement if $1=="*aaa" then print $4 from next line. I know that one of the solutions could be to convent file to this output:

*aaa A B C D E F 55

*ccc A B C R E T 33

using "*" as line separator, but I would like to avoid this.

Regards, lucas

lucas

Posted 2013-12-13T20:18:14.337

Reputation: 47

Answers

1

Use a variable (called primed in the example) which is set if the match is found. the next statement reads the next line which is then printed if primed is set.

awk '{
if (primed) print $4; 
if ($1 == "*aaa") { primed=1; next } primed=0; }' file

suspectus

Posted 2013-12-13T20:18:14.337

Reputation: 3 957

how can I replace next statement if I'm looking for a value from not the next line but next+1 or the previous line? – lucas – 2013-12-13T20:59:14.870

store all lines in an awk array (actually a hash table). Feel free to upvote or accept answer :) – suspectus – 2013-12-13T21:06:09.123