Sed script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed script
# 1  
Old 04-05-2006
Sed script

/\/\*/!b
:x
/\*\//!{
N
bx
}
s/\/\*.*\*\///

This scipt should remove c like commnets /**/

i know what de last line does
but i dont't know what the first lines do
Can anyone explain please
# 2  
Old 04-05-2006
If the comment spans for more than one line, then it will append all lines together to make it as a single line...

for example, if your program contains comment like

/** this is fist line
extended to next line
closed here **/

then the first section of sed script will convert it as single line

/** this is fist line extended to next line closed here **/

Then the last section of sed script will delete the anything in between /* and */


Here is how it works :
======================

example :

$more mah.txt
a
b
/*** this is a comment
extented to next line
and ends here ***/
c
d /** this is also comment **/
e
f
g
h


first line of sed script is "/\/\*/!b" - check for "/*" pattern in the line, if not found branch to the end of sed script, ie. don't do anything to the current line of input file and proceed to next line of input file... b denotes branch, hence there is no lable next to b, it will branch/go to the end of sed script

1st and 2nd line of input file (mah.txt) do not contain "/*", hence it won't do anything for the first two lines

now, the third line of input file contains "/*" ( "/*** this is a comment" ).. hence first line of the sed script fails, then go the next line in sed script and creates a lable :x ( you will use it later )...
next search for "*/" in the current line, if not found then append ( N in sed ) next line of the input file to current line ...
now 3rd and 4th line got appended as single line and now the current line contains "/*** this is a comment
extented to next line"
then branch/go to the lable x (ie bx in sed ).. it goes back to line :x in sed script

next line in the sed script again checks for "*/", if not found then append the next line again ( N in sed ), now along with 3rd and 4th line of input file, 5 line also will get appended... now the current line holds "/*** this is a commentextented to next lineand ends here ***/"
Next line in the sed script says brach/go back to lable x. Now again we are back to the lable :x, the next line sed script searchs for "*/", this time it matches and proceeds... and do a s/\/\*.*\*\/// which will delete the comment fully.


There is a good article on the net which explains in detail about the hold spaces, pattern spaces, branching etc..

http://www.grymoire.com/Unix/Sed.html
# 3  
Old 04-06-2006
is there a way to make this script replece te lines with empty lines
not just erase them

say the lines between comments
1: blabal /* bbbb
2: cccc
3: aaaaa
4: */ bla


want to erase this but i want also to preseve de lines as empty lines

something like this

1: blabal
2:
3:
4: bla

because I want to prezerve de actual nr of lines

or can i obtain somehow the line nrs

Last edited by clauchiorean; 04-06-2006 at 10:52 AM..
# 4  
Old 04-14-2006
sorry.. i'm not getting emails for the posts.. so i didn't check it

let me check

Last edited by mahendramahendr; 04-14-2006 at 07:51 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed script help

I am having a file as stated below : File 1: ########################## idnd a integer 2; list 1 ; list2 ; chip top alist( .a(1) , .b(2) , .c(3) , .d(1) , .e(7) , .n(80), .d(1) , .g(7) , .n(80), .f(1) , .e(7) , .m(80)); lis 7 nfj ; jdjd kn; jsjd l ; (4 Replies)
Discussion started by: kshitij
4 Replies

2. Shell Programming and Scripting

SED script

Hi , i am stuck in this simple script. #!/bin/ksh echo "enter the file name" read flname echo "enter version" read ver grep $flname /home/con/snsc/perl/map > flplist dirname `cat flplist` | sed 's/\/so${vers}\//\/so${vers}_xyz\//' > dirlist the map file is basically a list of file... (3 Replies)
Discussion started by: debu182
3 Replies

3. Shell Programming and Scripting

sed script

I am beginner for Unix. I practicing unix shell script. I worked out some sed script example from internet. Everything fine. But in real unix environment, where sed script is mainly used.? Can anyone give some examples for the usage of sed script in real unix environment. It will be useful for... (1 Reply)
Discussion started by: gwgreen1
1 Replies

4. Homework & Coursework Questions

Sed script

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Create a sed script that will display a list of all users in the /etc/passwd file that belong to the group... (0 Replies)
Discussion started by: lakers34kb
0 Replies

5. Shell Programming and Scripting

sed in script

I'm trying to write a simple script that replaces a string. The script works for uninterrupted strings, but as soon as sed encounters a space it stops reading the new string. I've tried double quotations in sed and backslashes before the $, however these don't work. Below is the script. Any help... (9 Replies)
Discussion started by: dsell002
9 Replies

6. UNIX for Dummies Questions & Answers

sed script

:rolleyes: I have a series of folders /temp/a /temp/b /temp/c In folders a, b, and c, I have files a1.txt..........a20.txt b1.txt..........b40.txt & c1.txt..........c60.txt Each file has the same data format :- Line... (2 Replies)
Discussion started by: grinder182533
2 Replies

7. Shell Programming and Scripting

sed script

how to convert the follow sed script file into a command line ? example: /^\.TS/,/^\.TE/{ /^$/p } I have tried the below but it is not working: # sed -n "/^\.TS/,/^\.TE/{/^$/p}" file file: 111 .TS 222 $333 << extract this line 444 .TE 555 (2 Replies)
Discussion started by: 3Gmobile
2 Replies

8. Shell Programming and Scripting

sed in a script

I am trying to run a sed command within a script to edit a file. I am trying to put the value of MYUSER into the sshd_config file. Instead of putting the value of the variable, MYUSER, it puts in the string ${MYUSER}. Anyone know a good solution to this? cat ${SSHD_CONFIG} | sed... (1 Reply)
Discussion started by: Mike_the_Man
1 Replies

9. UNIX for Dummies Questions & Answers

help in sed script

I am having a shell script that connects to database half hourly and pulls out the backlog from some tables. Now that logfile is growing too big and I need to housekeep it! effectively I want to keep last 30 days data in that file and move rest to archived file. The file contents are as below.... (14 Replies)
Discussion started by: abhi123
14 Replies

10. Shell Programming and Scripting

Sed script maybe?

I have a lot of script files that were created by Extract in a dir that no longer exists. Now that I have to run these scripts they 'all' have to be changed. I'm looking for a way to do a 'mass' change if possible. So far, I've dumped all of the script file names to a file and sorted them to... (5 Replies)
Discussion started by: HOlli
5 Replies
Login or Register to Ask a Question