Sed Script/Command Help


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Sed Script/Command Help
# 1  
Old 10-27-2004
Sed Script/Command Help

I want to write a sed script/command that will delete my comments from a C++ program.

Basically, I need to delete all the "/* random text */" from a file. How do I go about doing this? I've been reading some stuff online and can't figure it out. Thank you for your help!
# 2  
Old 10-27-2004
That:
Code:
sed -e 's;//.*$;;'

or that:
Code:
sed -e 's/\/\/.*$//'

. Same thing.
# 3  
Old 10-27-2004
Bug try this

Smilie sed 's/\/\*.*\*\///g' filename
moxxx68
# 4  
Old 10-27-2004
Oops. My examples were for // style comments...
# 5  
Old 10-27-2004
Or to take care of // and /* */ as well as multiline /* */

Code:
#!/usr/bin/sed -nf

# This first case takes care of the following two 
# scenarios:
# 1:  /* a comment spanning an entire line */
# 2:  int code = 1; /* code + comment on same line */
# We are saying search between /* and */, and then
# substitute that for "nothing". So the syntax
# /start/,/end/ searches between start and end.
/\/\*/,/\*\// {
  s/\/\*.*\*\///g
}

# take care of multiline comments
# This deletes any lines inclusive. So if a multiline
# comment started on a line with code also, this would
# break.
/\/\*/,/\*\// {
  d
}

# now for // style comments
# This just says substitute // followed by any number
# of anything, followed by the end of the line, with nothing.
s/\/\/.*$//g

# print the rest
p

EDIT: Added "special" case above and comments

Cheers
ZB

Last edited by zazzybob; 10-27-2004 at 08:53 AM..
# 6  
Old 10-27-2004
Quote:
s/\/\/.*$//g
dear zazzy bob
could you please explain this syntax..
i am not quite sure what it means..
also in the first example you used a , instead of .* what does the comma do?
thanx moxxx68
# 7  
Old 10-27-2004
No problem. I have recommented my original example above to clarify a few things.

Cheers
ZB
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need bash script to use a sed command as a variable

I need to be able to use a sed command as a variable in a bash script. I have the sed command that almost works the way I want it. the command is sed -n '/inet/,/}/p' config.boot This gets me this result: inet 192.168.1.245 } I need to get the IP address into a variable so I... (9 Replies)
Discussion started by: edlentz
9 Replies

2. Shell Programming and Scripting

sed command issue in script

Hi, I am using sed command to extract data from my log file for a certain time interval. From and To "time" are my input arguments. Now if i use the sed command on command line. I get the desired results and If i use it in script.It fails. sed command as command line: sed -n '/04-Mar-2015... (6 Replies)
Discussion started by: oberoi1403
6 Replies

3. UNIX for Dummies Questions & Answers

Script batch with command sed

hi, i have a folder with 2000 text file where each file contain a string. i need to trasform this string like this: example of file : My name is Mark and I'm a child the new file must be: insert into tabella ('My name','My name is Mark and I'm a child'); where the first column is a... (11 Replies)
Discussion started by: yo-yo78
11 Replies

4. Shell Programming and Scripting

sed command using variables in shell script

hi guys, The following command doesn't seem to work in my shell script: tag=$(sed -n '/${line}/ s/.*\.*/\1/p' myfile.txt) When i replace the ${line} with an actual value, it works fine. So, how do i use the ${line} in this sed command? Thanks in advance, Zaff (2 Replies)
Discussion started by: zaff
2 Replies

5. Shell Programming and Scripting

sed command in perl script

What is wrong with this line in a perl script? $amc_data = `sed -n '/\/,/\/p' "$config_file"` I ran the above from command line and it works fine from unix command prompt. The code should produce output between the and tags. The config_file is as follows: Sun ... (2 Replies)
Discussion started by: som.nitk
2 Replies

6. Shell Programming and Scripting

shell script/sed command help

First off I have read the man pages for sed and am still having trouble working on a script to remove portions of a log: My goal is to take a log file to be emailed, read the file and strip the portions away AFTER the line MIME-Version:1.0 and strip away until it to the line starting with... (4 Replies)
Discussion started by: murphybr
4 Replies

7. Shell Programming and Scripting

small sed script on command line.

Can anyone help me get this small sed script to work in shell on the command line? I need it in a one liner really as i want to edit many scripts in a for loop and dont want to have to invoke a separate script each time. #!/bin/sh sed '/mailx\ -s.*$ { i\ #Comment above mailx line ... (5 Replies)
Discussion started by: lavascript
5 Replies

8. Shell Programming and Scripting

Need help using sed command in shell script?

Hello, i want to take the input from user and according to that variable's value search in file emp.lst. Here is what i came up with echo -e "Enter string to be searched :\c" read str sed -n '/\$str/p' emp.lst this is not working! any idea why?Thanks in advance! :) (4 Replies)
Discussion started by: salman4u
4 Replies

9. Shell Programming and Scripting

Sed command in shell script

I have a current code working(named subst1) having a user be able to type this line to substitute words using the sed command: subst1 old-pattern new-pattern filename Here is my shell script: #!/bin/bash # subst1 ARGS=3 E_BADARGS=65 if then echo "Usage: `basename $0`... (1 Reply)
Discussion started by: Todd88
1 Replies

10. Shell Programming and Scripting

problem with sed command in shell script.

Guys, I've a problem in the "sed" command used in my shellscripts This is the problamatic line in my shell script: sed -e 's/${line1}/${line1_m}/g' prod_hier_1234.txt > test.txt It doesn't do the job of replacing the string stored in variable 'line1' to 'line1_m'. However If I replace the... (10 Replies)
Discussion started by: bhagat.singh-j
10 Replies
Login or Register to Ask a Question