-1

Let's say I have files 1.txt, 2.txt and 3.txt, all with the following contents respectively:

foo1

Notes

foo2
bar

Now, I want to print out lines, which are placed after the pattern Notes till the end of file, and contain either foo or bar patterns. To achieve that I run the following:

awk '/Notes/,/getline==0/ {if (/foo|bar/) {print}}' *txt

However, this range seems to be reset as soon as awk proceeds with the second file, since the result is the following (only foo2 and bar entries should be displayed):

foo2
bar
foo1
foo2
bar
foo1
foo2
bar

Obviously, if I do something like this:

for i in $(*txt); do awk '/Notes/,/getline==0/ {if (/foo|bar/) {print}}' $i; done

...everything will be sort of ok, but, of course, I don't want to start a new awk process for each file, so that's not really a solution.

My question is, how to tell awk to apply this search range /Notes/,/NR==0/ to all the files in a *txt mask/group?

2
  • /getline==0/ is searching for the pattern getline==0 - which never occurs Dec 10, 2019 at 11:05
  • @steeldriver, OK, but how do I define such range then?
    – A S
    Dec 10, 2019 at 11:14

1 Answer 1

2

/getline==0/ is searching for the pattern getline==0, which never occurs.

If you have GNU awk, you could do

gawk '/Notes/{notes=1} notes && /foo|bar/ {print} ENDFILE{notes=0}'

Otherwise (and more idiomatically)

awk 'FNR==1{notes=0} /Notes/{notes=1} notes && /foo|bar/'
1
  • Thanks, it works! I even knew this trick with setting a temp var at a pattern but was too stupid to apply it here :)
    – A S
    Dec 10, 2019 at 11:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .