1

Trying to remove the text between two strings (/* and */) in a txt file without deleting full lines.

Lets say I have a file containing the following:

/* reports */
%report1([email protected] /*[email protected] */,lag=3);
%report2([email protected] /*[email protected]
[email protected] */ ,lag=3);

My desired output would be

%report1([email protected], lag=3);
%report2([email protected]
,lag=3);

I have tried many combinations of tr, sed, and awk, but still not working. Any Thoughts? Notice that report 2 is on two different lines with the delimiters /* and */ also on separate lines.

1
  • Would you like the last lag=3to be behind %report2? Commented Nov 6, 2014 at 7:00

3 Answers 3

1

With :

perl -0777pe 's@/\*.*?\*/@@gs' file.txt
Sign up to request clarification or add additional context in comments.

2 Comments

@sputnick so ugly but yet so beautiful!
Strangely, it does not preserve the original line break (end of line) as requested by the question, in my test.
0
awk 'BEGIN { RS="/"; ORS="" }\
     /^\*/,/\*$/ { f=1; next }\
     { if (f!=0) f=0; else print "/"; print }' file.txt

Explanation

BEGIN { RS="/"; ORS="" } set the record separator to "/", and the output record separator to an empty string.

/^\*/,/\*$/ { f=1; next } skip any records between one that starts with *, and one that ends with *, while setting flag f to 1.

{ if (f!=0) f=0; else print "/"; print } if flag f is set then unset it, otherwise print /, and always print the record.

This does not handle the case of /* or */ occurring in a string.

Comments

0

GNU awk for multi-char RS:

$ awk -v RS='/[*]|[*]/' -v ORS= 'NR%2' file


%report1([email protected] ,lag=3);

%report2([email protected]  ,lag=3);

Comments

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.