
I'm curious to know of better approaches, though - there should be some. Not the most elegant by far, but should work. So the resulting pipe command to do what you want would look like: grep -A 1 "second" testfile | perl -npe '$x=1-$x s/\n/#UnUsedSequence#/ if $x ' | grep "#UnUsedSequence#third" | sed -e 's/#UnUsedSequence#/\n/' bands/Megadeth Drums:Dirk Verbeuren See that the -l option tells xargs to execute grep for every filename. bands/Kings X Drums:Jerry Gaskill grep Drums. bands/Rush Drums: Neil Peart grep Drums. bands/Dream Theater Drums:Mike Mangini grep Drums. You unwrap the unused sequences back into the newlines, sed might be the simplest: sed -e 's/#UnUsedSequence#/\n' /bands -type f xargs -d n -l -t grep Drums grep Drums. You do a grep on these lines, this time searching for string #UnUsedSequence#third: grep "#UnUsedSequence#third" It would in many ways break grep due to the fact mentioned above 1. bashrc etc, but dont unless you for some reason really understand the implications and still want to do it. export GREPOPTIONS'-coloralways' to your. I think the simplest way to do that would be using perl: perl -npe '$x=1-$x s/\n/#UnUsedSequence#/ if $x ' As mentioned by the good slm, you could add.

You translate every other newline into the sequence that is guaranteed not to occur in the input.

You match the line that contains "second" and output the line that has matched and the subsequent line: grep -A 1 "second" testfile Since the grep works on "lines" and these two are different lines, you would not be able to match it this way.
Your question abstract "bash grep newline", implies that you would want to match on the second\nthird sequence of characters - i.e. 'the last newline is trimmed' incorrectly implies that command substitution trims at most one trailing newline, when, in reality, 'sequences of one or more