1

I have multiple files where I am extracting text between two delimiters (START and END non-inclusive). After I process each file I want to output a delimiter indicating the file was processed, say the string "END". How would I add the string "END" to the output after each processed file? For example,

Say I have two files with the content

file1.txt

line 1
line 2
START
I want to extract this text
and this text
END
line 3
line 4

and

file2.txt

line10
line20
START
I also want this text.
END
line 30

I know if I execute the following:

awk '/START/,/END/{if (!/START/&&!/END/) print}' test*.txt

I will get the following output:

I want to extract this text
and this text
I also want this text.

But the output I want is:

I want to extract this text
and this text
END
I also want this text.
END

How do I get the string "END" added to the output after each file is processed?

GregH
  • 12,278
  • 23
  • 73
  • 109

3 Answers3

1
FNR==1 && NR!=1 {print "END"}
END {print "END"}

So you'd have:

awk 'FNR==1 && NR!=1 {print "END"} /START/,/END/{if (!/START/&&!/END/) print} END {print "END"}' test*.txt
Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
0

the variable FNR is reset to 0 on every new file, so you could have a rule trigger on (FNR==0 && NR!=0):

(FNR==0) && (NR!=0) {
  print "END"
}
END {
  print "END"
}
Ulrich Schwarz
  • 7,598
  • 1
  • 36
  • 48
0

You're asking awk to skip printing END. Stop that.

awk '/START/,/END/{if (!/START/) print}' test*.txt
glenn jackman
  • 238,783
  • 38
  • 220
  • 352