

Same as above but also print the 2 lines before and 2 lines after the line containing the string (total of 5 lines)? I'm thinking piping the line number and the filename into sed and printing all the required lines somehow. I'm having a problem with the order in which the grep output is generated.

Is there a way to combine both steps into one command? I'm thinking piping the line number and the filename into sed and printing the line.

I created the above commands manually by reading the line numbers and filenames Sed '9!d' /media/slowly/DATA/lots_of_files/lots_of_files/file_7.txt > print.txt
#Ls grep preserve newline how to#
I have also figured out how to print just the specific lines of a files containing the string using the following command: sed '3!d' /media/slowly/DATA/lots_of_files/lots_of_files/file_3.txt > print.txt media/slowly/DATA/lots_of_files/lots_of_files/file_7.txt:9:the_string is in this sentence too.Īs shown above, the output includes the filename, line number and all the text in that line including the string. media/slowly/DATA/lots_of_files/lots_of_files/file_7.txt:6:the_string is in this sentence. The output is as follows: /media/slowly/DATA/lots_of_files/lots_of_files/file_3.txt:3:the_string grep -nr "the_string" /media/slowly/DATA/lots_of_files > output.txt I use the following command to recursively search multiple files and find the line number in each file in which the string is found.
