How To Use sed To Remove Text Between '/*'?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How To Use sed To Remove Text Between '/*'?
# 1  
Old 03-20-2013
How To Use sed To Remove Text Between '/*'?

First of all let it be said that I hate sed. Every time I think I've got it figured out it proves me wrong. There, now with that out of the way here's my problem that I'm hoping one of you smart people can solve:

I have a file full of lines like this:
Code:
If x=1 then /* this is an if statement */
/* new line added */ echo "hello"

I want to remove the /* and */ everything between them too but leave the rest of the line intact.

Unfortunately this is proving really difficult since, based on the roughly 15 threads I've read about the topic of sed, both / and * seem to be special characters so using them as the search critiera is proving difficult for me to figure out. This is the closest I've gotten:
Code:
sed 's/*.**/*/g' results.tmp

But this only searches for the * and not both. It also wipes out everything to the right of the first * so that's not going to work.

Any help would be appreciated. Thanks.
# 2  
Old 03-20-2013
The following will work with the data sample you provided. However, it will not work with comments that span multiple lines. If you need something more complicated, you might want to look into using a tool that's familiar with the language in question.
Code:
sed 's|/\*[^*]*\*/||g'

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 3  
Old 03-20-2013
Quote:
Originally Posted by alister
The following will work with the data sample you provided. However, it will not work with comments that span multiple lines. If you need something more complicated, you might want to look into using a tool that's familiar with the language in question.
Code:
sed 's|/\*[^*]*\*/||g'

Regards,
Alister
Alister, you're amazing. Smilie

Can you do me one more favor and explain the syntax to me so I'll know for next time?
# 4  
Old 03-20-2013
Code:
/\*[^*]*\*/

/ - Begin search pattern

\* - Starts with asterisks * ( It is escaped \* because meta-character)

[^*] - Followed by any character other than ^ asterisks * (It not escaped because meta-characters loose there special meaning inside [ ])

* - Zero or more occurrences

\* - Followed by asterisks *

/ - End search pattern
This User Gave Thanks to Yoda For This Post:
# 5  
Old 03-20-2013
Quote:
Originally Posted by Korn0474
Alister, you're amazing. Smilie

Can you do me one more favor and explain the syntax to me so I'll know for next time?
After that characterization, how can I refuse. Smilie

You are correct in your original post when you say that / and * are both special to sed. However, sed's substitution command allows us to choose a delimiter other than /. For example, the following two commands are functionally identical, each replaces every occurrence of foo with bar:
Code:
sed 's/foo/bar/g'
sed 's|foo|bar|g'

In the second instance, I chose a pipe symbol, |, instead of the forward slash. The benefit of this choice is that if we need to use literal forward slashes within the s command, we don't have to precede them with a backslash. To change a/c to d/c, first with the default delimiter and then with the pipe symbol instead:
Code:
sed 's/a\/c/d\/c/g'
sed 's|a/c|d/c|g'

If you can imagine the number of forward slashes in something like a url (http://domain/dir/file), using a different delimiter can improve readability of the command.

While I chose a pipe in those examples, you can choose any character you like except a newline or a backslash. Whatever character you choose, you can still use it within the command, but you'll have to precede it with a backslash.

Breaking down the /\*[^*]*\*/ regular expression:
/\*: forward slash followed by a literal asterisk (which needs to be backslashed since it's normally a special character)
[^*]*: The expression in brackets means anything except an asterisk (which does not have to be backslashed because special characters are not special within the bracket expression). The asterisk that follows the closing bracket is indeed special. As a whole, this fragment means zero or more non-asterisks. This is used to ensure that we don't go past the end of the comment, which ends with a */ sequence.
\*/: This matches the */ sequence at the end of the comment. Backslash escape the asterisk so it's taken literally and follow it with the forward slash (which isn't special here and does not require a preceding backslash because we're using the pipe as a delimiter).

Regards,
Alister

---------- Post updated at 08:25 PM ---------- Previous update was at 08:23 PM ----------

Quote:
Originally Posted by Yoda
Code:
/\*[^*]*\*/

/ - Begin search pattern

/ - End search pattern
Thank you for helping with the explanation, Yoda. If I had known you were there, I would've saved myself some time. However, you misinterpreted the slashes at the beginning and the end. They are literal characters. The pipes delimit the regular expression.

Regards,
Alister

Last edited by alister; 03-20-2013 at 09:38 PM..
This User Gave Thanks to alister For This Post:
# 6  
Old 03-20-2013
Quote:
Originally Posted by James2rown
It also wipes out everything to the right of the first * so that's not going to work.ImageImageImage
ImageImageImage
To which poster's example are you referring to?
# 7  
Old 03-20-2013
Korn0474:

Ignore that post. That's probably a spammer in the making (already reported). That post is simply using words from your first post in this thread.

Regards,
Alister
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove bracket including text inside with sed

Hello, I could not remove brackets with text contents myfile: Please remove the bracket with text I wish to remove: I tried: sed 's/\//' myfile It gives: Please remove the bracket with text A1 I expect: Please remove the bracket with text Many thanks Boris (2 Replies)
Discussion started by: baris35
2 Replies

2. Shell Programming and Scripting

sed to remove text from file

Trying to use sed to, in-place, remove specific text from a file. Since there are / in the text I use | to escape that character. Thank you :). sed -i -e 's|xxxx://www.xxx.com/xx/xx/xxx/.*/|' /home/cmccabe/list sed: -e expression #1, char 51: unterminated `s' command (4 Replies)
Discussion started by: cmccabe
4 Replies

3. Shell Programming and Scripting

Remove text with sed or anything?

Hi there, my first post, so you might find my noobness ;) I need help to fix a thing, find /mnt/fs01/gfx/ -iname "*.rar" > gfx.txt Gives this contained textfile: /mnt/fs01/gfx/toba/toba.rar /mnt/fs01/gfx/zeta/zimba.rar /mnt/fs01/gfx/brashe/getha.rar And now the tricky part (for... (3 Replies)
Discussion started by: yabbah
3 Replies

4. Shell Programming and Scripting

sed to remove partial text in one line only

I have test.xml XML file like <Report account="123456" start_time="2014-09-08T00:00:00+00:00" end_time="2014-09-10T23:59:59+00:00" user="Dollar Tree" limit="1000000" more_sessions="some text "> <Session ......rest of xml............... I need output like <Report> <Session ......rest of... (3 Replies)
Discussion started by: kumars1331@gmai
3 Replies

5. Shell Programming and Scripting

Remove text from n position to n position sed/awk

I want to remove text from nth position to nth position couple of times in same line my line is "hello is there anyone can help me with this question" I need like this ello is there anyone can help me with question 'h' is removed and 'this' removed from the line. I want to do this... (5 Replies)
Discussion started by: elamurugu
5 Replies

6. Shell Programming and Scripting

Sed or awk script to remove text / or perform calculations from large CSV files

I have a large CSV files (e.g. 2 million records) and am hoping to do one of two things. I have been trying to use awk and sed but am a newbie and can't figure out how to get it to work. Any help you could offer would be greatly appreciated - I'm stuck trying to remove the colon and wildcards in... (6 Replies)
Discussion started by: metronomadic
6 Replies

7. Shell Programming and Scripting

sed to remove 1st two characters every line of text file

what is the sed command to remove the first two characters of every line of a text file? each line of the text file has the same amount of characters, and they are ALL NUMERIC. there are hundreds of lines though. for example, >cat file1.txt 10081551 10081599 10082234 10082259 20081134... (20 Replies)
Discussion started by: ajp7701
20 Replies

8. Shell Programming and Scripting

Remove text from a csv file using sed

I am trying to remove the ita from this file: "1234ita","john","smith" "56789ita","jim","thomas" the command i am using is: sed '/^ita/d' infile.csv > outfile.csv it is running but doing nothing, very rarely use sed so trying to learn it any help would be appreciated (2 Replies)
Discussion started by: Pablo_beezo
2 Replies

9. Shell Programming and Scripting

sed over writes my original file (using sed to remove leading spaces)

Hello and thx for reading this I'm using sed to remove only the leading spaces in a file bash-280R# cat foofile some text some text some text some text some text bash-280R# bash-280R# sed 's/^ *//' foofile > foofile.use bash-280R# cat foofile.use some text some text some text... (6 Replies)
Discussion started by: laser
6 Replies

10. Shell Programming and Scripting

How to use sed to remove html tags including text between them

How to use sed to remove html tags including text between them? Example: User <b> rolvak </b> is stupid. It does not using <b>OOP</b>! and should output: User is stupid. It does not using ! Thank you.. (2 Replies)
Discussion started by: alphagon
2 Replies
Login or Register to Ask a Question