Search code examples
unixgrepcathp-ux

Unix open file from line X or first 100 lines after Z found


I have a 2GB text file and I want to read every line 100 lines after a certain string is found example:

string: 'epicvar (string) ="5"'

string was found on line 5000 so I want to read the file from 5000 to 5100 then the string was also found on line 15000 so I want to read the file from 15000 to 15100 and so on.

The system used is HP-UX B.11.31 U


Solution

  • This may work for you (untested), if you understand ll2p means last line to printit should be easy enough to read...

    awk '/pattern/ {ll2p = NR+100}
            NR < ll2p {print}
            NR==ll2p {ll2p=0} ' file