How can I create a file that contains everything after and including a particular search term from another file in the terminal?


Original file content: foo, bar, baz, qux

Search term: baz

New file content: baz, qux


Using GNU grep, and the same example as in your question, the following would do:

$ echo 'foo, bar, baz, qux' | grep -o '\bbaz\b.*'
baz, qux

Option -o asks grep to print only the part of the line that matches, and \b ensures that baz is matched at word boundaries.

  • This goes part of the way to solving this, but wasn't usable for entries with multiple lines, and I also need to take one file as the input and another as the output.
    – Waddaah
    Dec 10 '15 at 11:32

In the end I used pcregrep for this. The best usage I found for returning everything after the search term in a multiline file is the below, substituting for searchTerm, originalFile and newFile:

pcregrep -M 'searchTerm((.|\n)*)' originalFile > newfile

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.