Search for Multiple Strings in Linux Command Line

Problem:  I have a text file with a lot of information, I need to pick lines of information containing a particular pattern(s) and store it in another file

Solution: Use egrep to filter out lines with required pattens and write the result in a file.

egrep is actually a short form for “grep -E”, a command used to handle pattens while searching.

For Eg.
Lets say you have a text file with a list of file names and you want to filter out only those names that are of a certian type. For the sake of this example lets assume you want to only see filenames of files ending with .mp3 or .wav or .ogg

The command you need to use is:

cat file | egrep “.mp3|.wav|.ogg” > musiclist.txt

Voila you now have a file that sorts out your music files.

Bonus: If you want to list out all the music files you have stored under a particular directory you can use the following command

ls -1R /path/to/music | egrep “.mp3|.wav|.ogg” > musiclist.txt

Reblog this post [with Zemanta]