See Context for Words Appearing in Files


-A, -B, -C

When dealing with data, context is everything. As you've learned, grep outputs the actual line containing the search term, but you can also tell grep to include lines before and after the match. In the last section, "Search the Output of Other Commands for Specific Words," you used grep to work with a list of John Coltrane albums. One of his best was A Love Supreme. What three albums came out before that one? To get the answer, use the -B (or --before-context=#) option.

$ ls -1 | grep -B 3 A_Love_Supreme 1963_Impressions 1963_John_Coltrane_&_Johnny_Hartman 1963_Live_At_Birdland 1964_A_Love_Supreme 


If you want to find out what came after A Love Supreme, use the -A (or --after-context=#) option instead.

$ ls -1 | grep -A 3 'A_Love_Supreme' 1964_A_Love_Supreme 1964_Coltrane's_Sound 1964_Crescent 1965_Ascension 


To get the full historical context for A Love Supreme, try the -C (or --context=#) option, which combines before and after.

$ ls -1 | grep -C 2 'A_Love_Supreme' 1963_John_Coltrane_&_Johnny_Hartman 1963_Live_At_Birdland 1964_A_Love_Supreme 1964_Coltrane's_Sound 1964_Crescent 


This can be a bit confusing when you have more than one match in a file or group of files. For instance, Coltrane released several live albums, and if you want to see the albums just before and after those, you're going to get more complex results.

$ ls -1 | grep -C 1 Live 1963_John_Coltrane_&_Johnny_Hartman 1963_Live_At_Birdland 1964_A_Love_Supreme -- 1965_Last_Trane 1965_Live_in_Seattle 1965_Major_Works_of_John_Coltrane -- 1965_Transition 1966_Live_at_the_Village_Vanguard_Again! 1966_Live_in_Japan 1967_Expression 1967_Olatunji_Concert_Last_Live_Recording 1967_Stellar_Regions 


The -- characters separate each matched group. The first two groups of results are obviousan album with Live in the title, preceded and followed by another albumbut the last section is a bit more complicated. Several albums containing the word Live in the title are right next to each other, so the results are bunched together. It might look a bit weird, but if you look at each instance of Live, you'll notice that the album before and after it is in fact listed.

The results are even more informative if you incorporate the -n option, which lists line numbers (because you're using ls -1, it's the line number of that ls listing).

$ ls -1 | grep -n -C 1 Live 37-1963_John_Coltrane_&_Johnny_Hartman 38:1963_Live_At_Birdland 39-1964_A_Love_Supreme -- 48-1965_Last_Trane 49:1965_Live_in_Seattle 50-1965_Major_Works_of_John_Coltrane -- 52-1965_Transition 53:1966_Live_at_the_Village_Vanguard_Again! 54:1966_Live_in_Japan 55-1967_Expression 56:1967_Olatunji_Concert_Last_Live_Recording 57-1967_Stellar_Regions 


Now -C gives you even more information about each line, as indicated by the character after the line number. A : indicates that the line matches, while a - means that it's a line before or after a match. Line 54, 1966_Live_in_Japan, does double duty. It comes after 1966_Live_at_the_Village_Vanguard_Again!, which should mean it has a -, but it is itself a match, which necessitates a :. Because a match is more important, that wins, and a : is ultimately used.



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