Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get everything in a file after a grep'd string

Yesterday a situation came up where someone needed me to separate out the tail end of a file, specified as being everything after a particular string (for sake of argument, "FOO"). I needed to do this immediately so went with the option that I knew would work and disregarded The Right Way or The Best Way, and went with the following:

grep -n FOO FILE.TXT | cut -f1 -d":" | xargs -I{} tail -n +{} FILE.TXT > NEWFILE.TXT

The thing that bugged me about this was the use of xargs for a singleton value. I thought that I could go flex my Google-Fu on this but was interested to see what sort of things people out in SO-land came up with for this situation

like image 828
geoffjentry Avatar asked May 11 '11 17:05

geoffjentry


People also ask

How do you grep everything in a file?

Search All Files in Directory To search all files in the current directory, use an asterisk instead of a filename at the end of a grep command. The output shows the name of the file with nix and returns the entire line.

How do I find all files containing specific text on Linux?

Grep Command. grep is a built-in Linux command that prints lines that match a given pattern. It returns all the lines of a file that contain a certain string by default, and the command is also case-sensitive.

How do I print 10 lines after grep?

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. Save this answer.

How do I find all files containing specific text on CMD?

Search for a text string in a file & display all the lines where it is found. "string" The text string to find (must be in quotes). [pathname] A drive/file(s) to search (wildcards accepted). /V Display all lines NOT containing the specified string. /C Count the number of lines containing the string.


1 Answers

sed -n '/re/,$p' file

is what occurs to me right off.

like image 72
geekosaur Avatar answered Sep 28 '22 20:09

geekosaur