[Editorial insertion: Possible duplicate of the same poster\'s earlier question?]
Hi, I need to extract from the file:
first
second
grep -A1 "second" | grep -B1 "third" works nicely, and if you have multiple matches it will even get rid of the original -- match delimiter
you could use
$ grep -1 third filename
this will print a string with match and one string before and after. Since "third" is in the last string you get last two strings.
So you just don't want the line containing "first"? -v
inverts the grep results.
$ echo -e "first\nsecond\nthird\n" | grep -v first
second
third
grep -v '^first' filename
Where the -v flag inverts the match.
Instead of grep
, you can use pcregrep
which supports multiline patterns
pcregrep -M 'second\nthird' file
-M
allows the pattern to match more than one line.
I don't really understand what do you want to match. I would not use grep, but one of the following:
tail -2 file # to get last two lines
head -n +2 file # to get all but first line
sed -e '2,3p;d' file # to get lines from second to third
(not sure how standard it is, it works in GNU tools for sure)