2
votes

I have a big file of 5000+ lines which has a repeated pattern like shown below:

ABC
111
222
333
XYZ

ABC
444
555
666
777
XYZ

..
..

ABC
777777777
888888888
999999999
222
333
111
XYZ

I would like to extract contents between each 'ABC' and 'XYZ' and write it to a separate file.

Ex: file1 should have

ABC
111
222
333
XYZ

File2 should have

ABC
444
555
666
777
XYZ

Filen should have

ABC
777777777
888888888
999999999
222
333
111
XYZ

and so on.

How could we achieve this ? I read these below threads but it writes only one single file. Didn't help for my case.

How to select lines between two marker patterns which may occur multiple times with awk/sed

Print lines between two patterns to new file

3

3 Answers

4
votes
awk '/^ABC/{file="file"c++}{print >>file}' a
2
votes

Perl to the rescue!

< bigfile perl -nwe 'print {$OUT} $_
                         if (/ABC/ && do { open $OUT, ">", "file" . ++$i or die $!}
                            ) ... /XYZ/'
  • n reads the file line by line
  • it only prints if between /ABC/ and /XYZ/
  • when /ABC/ is true, i.e. we're starting a new section, a new file is opened and associated with the filehandle $OUT. $i is the number of the file.
1
votes
awk '
  # setup our output file name file0, file1, file2, ...
  $0 == "ABC"{if (i) {close(f)};f="file"i++;};
  # use inclusive range match 
  $0 == "ABC",$0 == "XYZ"{print > f}
'