How to grep a string after a specified line number? How to grep a string after a specified line number? linux linux

How to grep a string after a specified line number?


You can tail it, then grep:

tail -n +50000 myfile.txt | grep -in "time spent"


Answer for grepping between any 2 line numbers:

Using sed and grep:sed -n '1,50000p' someFile | grep < your_string >


Alternatively you can use sed.sed can be used to mimic grep like this:

sed -n 's/pattern/&/p'

By default sed prints every line even if no substitution occurs. The combinations of -n and /p makes sed print only the lines where a substitution has occured. Finally, we replace pattern by & which means replace pattern by itself. Result: we just mimicked grep.

Now sed can take a range of lines on which to act. In your case:

sed -n '50000,$s/time spent/&/p' myfile.txt

The format to specify the range is as follow: start,endWe just instruct sed to work from line 50000 to $ which means last line.