How can I append lines after a pattern match in awk based on how many lines they are after the pattern match.
What I have looks like this:
Stuff
PATTERN
a b c
a b c
a b c
ENDOFSECTION
Morestuff
I would like to add various things to the of end of the lines shown as "a b c" (without knowing what a b c are specifically i.e., they are arbitrary).
Desired output:
Stuff
PATTERN
a b c k
a b c k
a b c t
ENDOFSECTION
Morestuff
I think this will require some sort of loop that counts lines after the pattern is recognised. My issue is I don't know how to link the counting of lines to variable after a pattern search.
My very rough idea so far (which doesn't work):
awk '/PATTERN/ {
i=1
Do
{print $0, " k"
i++
} while (i<=2)
}
k
ort
? It sounds like all you need isawk '/foo/{i=1}/ENDOFSECTION/{i=0} {if(i==1){print $0,k}else{print}}'
Am I missing something?k
is not set.Changek
to"foo"
to test it. What we need to understand is whenk
should be added and whent
. How can we know? Costas's answer assumes that if there are more than two lines, you wantt
and if less,k
. Is that correct?k
" are and what logic the script would use to know which one to append.