Ubuntu – How to display the rest of a file starting from a matching line

command linetext processing

I want to display a line matching a search term and all the remaining lines in the file.

For example, if I use:

more text.txt | egrep show

it will only show the line that has "show" in it, while I would wish for something like a start command:

more text.txt | start show

that will show all the lines after the first line that matches the search term.

However, it's not working on my Ubuntu. How can I install it or anything similar to it?

Best Answer

If your file is less than, say, 10000 lines, you can do this:

grep -A 10000 show text.txt

The -A flag will show the line containing the search string (show) and the next 10000 lines.

However, I played around with the -A flag a bit, and I noticed grep will intelligently combine the output, so you don't get 10001 lines for each time the string show is found. So basically, it will show you the whole file once, starting from the line that contains show. If your file contains more than 10000 lines, adjust the parameter appropriately.

You can use the alias command to create your own custom command to achieve the same result.

EDIT: a slightly more elegant solution would be to use

 grep show -A $(wc -l < text.txt) text.txt

which will use the actual length of the file as the -A flag. This requires you to specify the file name twice. Unfortunately this will prevent the use of an alias, but you could write a shell function to do this.

Apparently, in this case you need to specify the search string first to avoid an error.