Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find line number in a text file - without opening the file

Tags:

linux

In a very large file I need to find the position (line number) of a string, then extract the 2 lines above and below that string.

To do this right now - I launch vi, find the string, note it's line number, exit vi, then use sed to extract the lines surrounding that string.

Is there a way to streamline this process... ideally without having to run vi at all.

like image 829
Eleco Avatar asked Oct 05 '12 16:10

Eleco


1 Answers

Maybe using grep like this:

grep -n -2 your_searched_for_string  your_large_text_file

Will give you almost what you expect

-n : tells grep to print the line number

-2 : print 2 additional lines (and the wanted string, of course)

like image 141
mbarthelemy Avatar answered Oct 02 '22 14:10

mbarthelemy