0

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!

1
  • Your question is a little vague to me,can you post the current grep command your using? Commented Oct 5, 2012 at 17:39

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

This is great! Is it hard if I want add \ back?
Probably easiest to not delete it in the first place. Use awk '{ while( /\\$/ ) ... instead.
1

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

2 Comments

+1 for the answer. Shouldn't you use -A1 instead of -C1 so it matches what the OP wanted.
Thanks for the answer. What if the bash file contains: ls \ /var \ /tmp

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.