Sunday, January 28, 2024

[SOLVED] sed search multiple strings in one file

Issue

I want to search multiple strings in the same file So far I have this working but one string only

sed -n '/XXX/,+1p'  FILE > FILE

But I want

sed -n '/XXX/YYY/ZZZ/,+1p'  FILE > FILE

I could not make it work.


Solution

Use \| to separate multiple patterns to match.

sed -n '/XXX\|YYY\|ZZZ/,+1p'  INFILE > OUTFILE

Also, the input file has to be different from the output file (if you want to overwrite the file you should use the -i option rather than redirecting to the input file).



Answered By - Barmar
Answer Checked By - Katrina (WPSolving Volunteer)