0

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)
}
  • I don't understand why you need to count the lines. Also, where is the extra input coming from? How can the script know whether to add k or t? It sounds like all you need is awk '/foo/{i=1}/ENDOFSECTION/{i=0} {if(i==1){print $0,k}else{print}}' Am I missing something? – terdon Mar 9 '15 at 17:38
  • I was thinking it was perhaps the only way to do it. Counting the lines isn't a big deal if the output can be made. k and t were just to show two different things added to the end of the line (my idea isn't implemented properly as I don't know how). Your line doesn't produce the output. – Christopher Mar 9 '15 at 17:41
  • 1
    No, because k is not set.Change k to "foo" to test it. What we need to understand is when k should be added and when t. How can we know? Costas's answer assumes that if there are more than two lines, you want t and if less, k. Is that correct? – terdon Mar 9 '15 at 17:47
  • 2
    OK, but based on what? How can the script know which one to add? Do you want to be prompted each time? – terdon Mar 9 '15 at 17:51
  • 1
    Please edit your question and give a more concrete example. The basic idea is what both answers you have received show. If you need something more specific than that, you will need to explain what exactly these "blocks of k" are and what logic the script would use to know which one to append. – terdon Mar 9 '15 at 17:56
3
awk '
  BEGIN {split("ABCDEFGH", new, "")}
  /ENDOFSECTION/ {addcol=0}
  addcol {$(++NF) = new[++i]}
  /PATTERN/ {addcol=1; i=0}
  {print}
' file
Stuff
PATTERN
a b c A
a b c B
a b c C
ENDOFSECTION
Morestuff

The BEGIN block is an example of how to create an array of new stuff to add.

Then, the awk program acts like a state machine, setting/unsetting a flag when certain patterns are seen.

  • Thank you, this works for my problem as I can change the ABCDEFGH to whatever I need it to be. – Christopher Mar 9 '15 at 17:59
2

Based on what's been clarified thus far...

awk '
    BEGIN { a="ktp" }
    /ENDOFSECTION/ { f=0 }
    f { $0=$0 "   " substr(a,++c,1) }
    /PATTERN/ { f=1 }
    { print }
'
1
awk '
/PATTERN/,/ENDOFSECTION/{
    if(/PATTERN/){
        i=0
        print
        next
    }
    if(!/ENDOFSECTION/){
        ++i
        if(i<3)
            $(NF+1)="\tk"
        else
            $(NF+1)="\tt"
    }
}
1'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.