Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to grep a string after a specified line number?

Tags:

I have a large text file, I want to see the lines containing "time spent" in this text file, I use:

grep -in "time spent" myfile.txt 

But I'm interested only in the lines after 50000. In the output I want to see lines after 50000 and that contain "time spent". Is there a way to do this?

like image 798
alwbtc Avatar asked Oct 28 '12 14:10

alwbtc


People also ask

How do you grep after a line?

You can use grep with -A n option to print N lines after matching lines. Using -B n option you can print N lines before matching lines. Using -C n option you can print N lines before and after matching lines.

How do I grep a specific line number in Linux?

The -n ( or --line-number ) option tells grep to show the line number of the lines containing a string that matches a pattern. When this option is used, grep prints the matches to standard output prefixed with the line number.

How do I grep certain lines in a file?

The grep command searches through the file, looking for matches to the pattern specified. To use it type grep , then the pattern we're searching for and finally the name of the file (or files) we're searching in. The output is the three lines in the file that contain the letters 'not'.

Can grep show line numbers?

To Display Line Numbers with grep MatchesAppend the -n operator to any grep command to show the line numbers. We will search for Phoenix in the current directory, show two lines before and after the matches along with their line numbers.


1 Answers

You can tail it, then grep:

tail -n +50000 myfile.txt | grep -in "time spent" 
like image 139
adray Avatar answered Nov 01 '22 12:11

adray