Reputation: 743
So I feel like the title is self explanatory. I'm new to sed and am having trouble with the syntax to find multi character patterns like // and delete the pattern all the way to the end of the line. For example, I would like this
// here's some stuff
here's some other stuff // here's even more stuff
to turn into this
here's some other stuff
I've tried sed -i -e '/\/\*/d' $FILENAME
, but that's not working.
Upvotes: 0
Views: 4263
Reputation: 10110
sed -i 's/\/\/[^\/\/]\+$//' $FILENAME
the above code would delete the string between the last occurrence of //
until the end of the line.
To delete the string from the first occurrence of //
use the following as suggested in the comments:
sed -i 's/\/\/.*$//' $FILENAME
Upvotes: 0
Reputation: 1195
sed -i -e 's|//.*||' -e '/^[[:space:]]*$/d' $FILENAME
Using the | lets you not worry about the / in your pattern. The second one just deletes any ensuing lines created that only contain zero or more spaces.
echo -e '// some first comment\nsome comment // here is the comment' | sed -e 's|//.*||' -e '/^[[:space:]]*$/d'
some comment
This isn't as neat as another answer here, but it's fairly easy to read and understand.
[updated to handle deleting blank lines]
Upvotes: 2
Reputation: 785276
You can use this sed command with alternate regex delimiter ~
:
sed -i.bak '\~^[[:blank:]]*//~d; s~//.*~~' file
here's some other stuff
\~^[[:blank:]]*//~d
is used to delete all lines where //
is at the start (or after some whitespaces at start).
s~//.*~~
is used to remove text starting from //
where //
comes in the middle.
Upvotes: 4