I have a file with about 1000 lines. I want the part of my file after the line which matches my grep statement.
i.e.
$ cat file | grep 'TERMINATE' // Its found on line 534
So, I want the file from line 535 to line 1000
for further processing.
How do I do it ?
If for any reason, you want to avoid using sed, the following will print the line matching
TERMINATE
till the end of the file:and the following will print from the following line matching
TERMINATE
till the end of the file:It takes 2 processes to do what sed can do in one process, and if the file changes between the execution of grep and tail, the result can be incoherent, so I recommend using sed. Moreover, if the file dones not contain
TERMINATE
, the 1st command fails.Alternatives to the excellent
sed
answer by jfgagne, and which don't include the matching line :awk '/TERMINATE/ {y=1;next} y'
( https://stackoverflow.com/a/18166628 )awk '/TERMINATE/ ? c++ : c'
( https://stackoverflow.com/a/23984891 )perl -ne 'print unless 1 .. /TERMINATE/'
( https://stackoverflow.com/a/18167194 )Use bash parameter expansion like the following:
grep -A 10000000 'TERMINATE' file
sed is a much better tool for the job: sed -n '/re/,$p' file
where re is regexp.
Another option is grep's --after-context flag. You need to pass in a number to end at, using wc on the file should give the right value to stop at. Combine this with -n and your match expression.
There are many ways to do it with
sed
orawk
:This looks for
TERMINATE
in your file and prints from that line up to the end of the file.This is exactly the same behaviour as
sed
.In case you know the number of the line from which you want to start printing, you can specify it together with
NR
(number of record, which eventually indicates the number of the line):Example