Search for Words Inside Search Results


grep | grep

What if you want a list of albums released by John Coltrane in the last two years of his career? Simple enough.

$ ls -1 | grep 196[6-7] 1966_Live_at_the_Village_Vanguard_Again! 1966_Live_in_Japan 1967_Expression 1967_Olatunji_Concert_Last_Live_Recording 1967_Stellar_Regions 


The range [5-7] limits what would otherwise be a much longer list to the years 19661967. So far, so good, but what if you don't want to include any of his live albums (which would normally be a horrible mistake, but let's pretend here)? Here's how to do it:

$ ls -1 | grep 196[6-7] | grep -v Live 1967_Expression 1967_Stellar_Regions 


The -v option (which you learned about previously in "Show Lines Where Words Do Not Appear in Files") worked to strip out lines containing Live, but the really interesting thing here is how you took the output of ls -1, piped that to grep 196[6-7], and then piped the output from that filter to a second instance of grep, this one with -v Live. The final results are exactly what you wanted: a list of all John Coltrane's albums, released between 19661967, that do not contain Live in the title. And that, my friends, shows you the power of the Linux command line in a nutshell!



Linux Phrasebook
Linux Phrasebook
ISBN: 0672328380
EAN: 2147483647
Year: 2007
Pages: 288

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net