echo
echo

Reputation: 799

Grep output pattern

If I have a bash file with following content:

ls \
/tmp

Is there a way to grep this file and get

ls \
/tmp

instead of

ls \

?

Please notice that other behaviors of grep should remain same if input is a large file and has other match patterns.

Thanks!

Upvotes: 0

Views: 195

Answers (2)

nneonneo
nneonneo

Reputation: 179392

You can use the context options, -A (lines after), -B (lines before), and -C (lines context, both before and after):

$ grep -A1 ls test.sh
ls \
/tmp

Upvotes: 1

William Pursell
William Pursell

Reputation: 212218

Assuming you want to treat the final \ as a line continuation character, you can use awk to concatenate the lines:

awk '{ while( sub( "\\\\$", "" )) { getline n; $0 = $0 n; }} /ls/' input-file

This removes the line continuation character and combines everything into one line, then prints the line if if matches the regex ls.

Upvotes: 1

Related Questions