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 I understand your question correctly you do want the lines after
TERMINATE
, not including theTERMINATE
-line.awk
can do this in a simple way:Explanation:
if(found) print
) will not print anything to start off with.This will print all lines after the
TERMINATE
-line.Generalization:
Example:
Explanation:
found
is set.found=1
so that the following lines are printed. Note that this check is done after the actual printing to exclude the start-line from the result.Notes:
BEGIN{found=0}
to the start of the awk-expression.A tool to use here is awk:
How does this work:
The other solutions might consume a lot of memory if you use them on very large files.
This could be a one way of doing it. If you know what line of the file you have your grep word and how many lines you have in your file:
grep -A466 'TERMINATE' file
The following will print the line matching
TERMINATE
till the end of the file:Explained:
-n
disables default behavior ofsed
of printing each line after executing its script on it,-e
indicated a script tosed
,/TERMINATE/,$
is an address (line) range selection meaning the first line matching theTERMINATE
regular expression (like grep) to the end of the file ($
), andp
is the print command which prints the current line.This will print from the line that follows the line matching
TERMINATE
till the end of the file:(from AFTER the matching line to EOF, NOT including the matching line)
Explained:
1,/TERMINATE/
is an address (line) range selection meaning the first line for the input to the 1st line matching theTERMINATE
regular expression, andd
is the delete command which delete the current line and skip to the next line. Assed
default behavior is to print the lines, it will print the lines afterTERMINATE
to the end of input.Edit:
If you want the lines before
TERMINATE
:And if you want both lines before and after
TERMINATE
in 2 different files in a single pass:The before and after files will contain the line with terminate, so to process each you need to use:
Edit2:
IF you do not want to hard-code the filenames in the sed script, you can:
But then you have to escape the
$
meaning the last line so the shell will not try to expand the$w
variable (note that we now use double quotes around the script instead of single quotes).I forgot to tell that the new line is important after the filenames in the script so that sed knows that the filenames end.
Edit: 2016-0530
Sébastien Clément asked: "How would you replace the hardcoded
TERMINATE
by a variable?"You would make a variable for the matching text and then do it the same way as the previous example:
to use a variable for the matching text with the previous examples:
The important points about replacing text with variables in these cases are:
$variablename
) enclosed insingle quotes
['
] won't "expand" but variables insidedouble quotes
["
] will. So, you have to change all thesingle quotes
todouble quotes
if they contain text you want to replace with a variable.sed
ranges also contain a$
and are immediately followed by a letter like:$p
,$d
,$w
. They will also look like variables to be expanded, so you have to escape those$
characters with a backslash [\
] like:\$p
,\$d
,\$w
.These will print all lines from the last found line "TERMINATE" till end of file:
As a simple approximation you could use
which greps for
TERMINATE
and outputs up to 100000 lines following that line.From man page