41

I know that by using the "-A NUM" switch I can print specific number of trailing lines after each match. I am just wondering if it's possible to print trailing lines until a specific word is found after each match. e.g. When I search for "Word A" I want to see the line containing "Word A" and also the lines after it until the one containing "Word D".

context:

Word A
Word B
Word C
Word D
Word E
Word F

command:

grep -A10 'Word A'

I need this output:

Word A
Word B
Word C
Word D
68

It seems that you want to print lines between 'Word A' and 'Word D' (inclusive). I suggest you to use sed instead of grep. It lets you to edit a range of input stream which starts and ends with patterns you want. You should just tell sed to print all lines in range and no other lines:

sed -n -e '/Word A/,/Word D/ p' file
  • Any tips on how to make it exclusive (for any generic situation, not just OP's)? – 2rs2ts Jun 18 '13 at 17:18
  • 4
    @2rs2ts to make it exclusive, just add | sed -e '1d;$d', that is remove first and last line – holroy Jun 26 '15 at 9:54
  • And if you want to exclude all lines between -- that is, you want to just see lines that are outside the two matches -- then: sed -n -e '/pattern A/,/pattern D/d; p' This says, "delete from pattern A to pattern D and print everything else". Unfortunately, this match is greedy. That means if patterns A and D appear more than once, you're going to match from the first pattern A to the last pattern D. I'm afraid Perl or Python are your friends if that's the case. – fbicknel Dec 19 '18 at 19:07
14

why not use awk ?

awk '/Word A/,/Word D/' filename
  • 2
    sed appears to be able to do this much more efficiently if a large number of files are involved. awk may be easier to remember, but sed seems to be worth a sticky note in my brain. – JimNim Nov 24 '14 at 16:02
  • 1
    awk is superior if Word D falls on the same line as Word A, and possibly elsewhere, eg Word A Word D\nWord D with sed will show two lines where as awk will show one. Additionally one can still use variables with awk, eg awk -v _word="Word A" '$0 ~ _word,/Word D/' "/some/file", so sed maybe more efficient but when dealing with searches of two strings that may or may-not fall on the same line, awk definitely be more reliable. – S0AndS0 Oct 26 '18 at 20:50
7
perl -lne 'print if /Word A/ .. /Word D/' file

or

cat file | perl -lne 'print if /Word A/ .. /Word D/'
  • 1
    +1 to counter the drive-by downvote. I'd still use sed for this, unless you need the power of Perl regular expressions to select the delimiting lines. – tripleee Jul 2 '13 at 9:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.