Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

The sed command

Status
Not open for further replies.

adelinewss

Programmer
May 22, 2000
20
SG
Hi,<br>I need to delete the content of files which start with /* and end with */.&nbsp;&nbsp;Like example:- <br><br>/* Today is Friday<br>Now is 3p.m. */<br><br>And also I need to delete the line from the start point with &quot;//&quot;.&nbsp;&nbsp;Like example:-<br><br>Today is Friday.&nbsp;&nbsp;//Now is 3p.m.<br><br>I want to delete //Now is 3p.m. onwards.&nbsp;&nbsp;<br><br>May I know how to do this with sed or other UNIX command?<br><br>Thks,<br>Adeline<br>
 
You could do something for this in perl.&nbsp;&nbsp;(I used to do something similar to strip customer dependent source code from files, so this should work.)<br><FONT FACE=monospace><br>#!/usr/bin/perl<br><br>$Input = &quot;/path/to/input/file&quot;;<br>$Output = &quot;/path/to/new/output/file&quot;;<br><br>open(INPUT, &quot;&lt;$Input&quot;);<br>open(OUTPUT, &quot;&gt;$Output&quot;);<br><br># Loop thru the input file assigning each line to<br># $Current_Line to avoid messing around with $_.<br>while ($Current_Line = &lt;INPUT&gt;) {<br><br>&nbsp;&nbsp;&nbsp;&nbsp;# If the current line starts with a &quot;comment start&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;# indicator, set the &quot;in a comment&quot; flag.<br>&nbsp;&nbsp;&nbsp;&nbsp;if ( $Current_Line =~ /\/\*/ ) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$In_A_Comment = 1;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;# Are we in a comment section?<br>&nbsp;&nbsp;&nbsp;&nbsp;if ( $In_A_Comment == 1 ) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# See if we are at the end of the comment section.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ( $Current_Line =~ /\*\// ) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Reset &quot;in a comment&quot; indicator.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$In_A_Comment = 0;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Skip this line.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;next;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else {<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# In a comment, so skip current line<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;next;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;# For all lines, strip data following (and including)<br>&nbsp;&nbsp;&nbsp;&nbsp;# &quot;//&quot; from input lines.<br>&nbsp;&nbsp;&nbsp;&nbsp;$Current_Line =~ s/\/\/.*//;<br><br>} # End &quot;while&quot;<br></font><br><br>Hope this helps.
 
like the perl script Andy - you should put that as a FAQ in Perl (mind you - &lt;grin&gt; - you don't have a 'print OUTPUT &quot;$Current_Line\n&quot;;' line - or am I missing something?)<br><br>If you don't have perl - think you can use the C ProProcessor (cpp) and that will strip C style comments (not C++ // comments though). <p>Mike<br><a href=mailto:michael.j.lacey@ntlworld.com>michael.j.lacey@ntlworld.com</a><br><a href= Cargill's Corporate Web Site</a><br>Please -- Don't send me email questions without posting them in Tek-Tips as well. Better yet -- Post the question in Tek-Tips and send me a note saying "Have a look at so-and-so in the thingy forum would you?"
 
Doh!&nbsp;&nbsp;You're right, Mike, I missed out a crucial little line about actually sending the stuff we <i>do</i> want to keep to the new file!<br><br>After the line:<br><FONT FACE=monospace><br>&nbsp;&nbsp;&nbsp;&nbsp;$Current_Line =~ s/\/\/.*//;<br></font><br>you want to add:<br><FONT FACE=monospace><br>&nbsp;&nbsp;&nbsp;&nbsp;print OUTPUT $Current_Line;<br></font><br>Also, if you want to be tidy you could add <FONT FACE=monospace>close(INPUT);</font> and <FONT FACE=monospace>close(OUTPUT);</font> at the very end of the script.<br><br>Thanks for the heads up Mike - I guess that will teach me to post replies late on a Friday afternoon :)<br><br>By the way, I'm still looking at sed/awk solutions to this, and if I get one I'll post it here.
 
You're right about the close statements.<br><br>I used to not bother - but got bitten with a perl script called once a second by cron that opened multiple files.<br><br>Worked fine - for months.<br><br>Upgraded to HPUX 10. (something - what I was on before 10.20) and *BANG* as the whole system ran out of file handles.<br><br>Whoops.....<br><br>So - now I tend to close files when I'm done with them<br> <p>Mike<br><a href=mailto:michael.j.lacey@ntlworld.com>michael.j.lacey@ntlworld.com</a><br><a href= Cargill's Corporate Web Site</a><br>Please -- Don't send me email questions without posting them in Tek-Tips as well. Better yet -- Post the question in Tek-Tips and send me a note saying "Have a look at so-and-so in the thingy forum would you?"
 
Getting rid of the // is easy using sed: -<br><br>sed '/\/\//d' &lt;oldfile&gt;newfile<br><br>If you want to delete the total content of the file then this will work in korn shell: -<br><br>head -1 oldfile ¦ grep &quot;^\/\*&quot; &gt; testfile <br>if [ -s testfile ] <br>then <br>tail -1 oldfile ¦ grep &quot;\*\/$&quot; &gt; testfile<br>if [ -s testfile ] <br>then <br>&gt; oldfile <br>echo &quot;file initialised&quot; <br>else <br>echo &quot;file end ok&quot; <br>fi <br>else <br>echo &quot;file start ok&quot; <br>fi<br><br>Note; in grep and sed the $ is the metacharacter for end of line and the ^ is for start of line, the \ negates the metacharacter so that it is read as it is. <br><br> <p>Ged Jones<br><a href=mailto:gedejones@hotmail.com>gedejones@hotmail.com</a><br><a href= > </a><br>
 
It is not pretty, but:<br><br>sed -e '/^\/\*/d' -e '/\*\/$/d' -e 's/\/\/[&nbsp;&nbsp;&nbsp;-z]*//' -e '/^$/d' $INFILE &gt; $OUTFILE<br><br>should work.&nbsp;&nbsp;The character in the [&nbsp;&nbsp;&nbsp;-z] test is a tab.
 
That is -- if I understand what you want.&nbsp;&nbsp;My quick test produced:<br><br># cat t<br>/* comment 1<br>comment 2 */<br>text<br>text 2 // comment3<br>// comment4<br>text 3<br>text 4 //&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;comment4<br><br># sed -e '/^\/\*/d' -e '/\*\/$/d' -e 's/\/\/[&nbsp;&nbsp;&nbsp;-z]*//' -e '/^$/d' t &gt; tt<br><br># cat tt<br>text<br>text 2<br>text 3<br>text 4
 
Wow! I *like* that Caliban... Obfuscation rules, but it's *very* cool <p>Mike<br><a href=mailto:michael.j.lacey@ntlworld.com>michael.j.lacey@ntlworld.com</a><br><a href= Cargill's Corporate Web Site</a><br>Please -- Don't send me email questions without posting them in Tek-Tips as well. Better yet -- Post the question in Tek-Tips and send me a note saying "Have a look at so-and-so in the thingy forum would you?"
 
Thanks a lot for your help.&nbsp;&nbsp;It's greatly appreciated.
 
Sorry, I made a slight omission, to delete all text lines from // onward use: -<br>sed '/\/\/.*/d' &lt;oldfile&gt;newfile<br>I MISSED THE .*<br><br><br> <p>Ged Jones<br><a href=mailto:gedejones@hotmail.com>gedejones@hotmail.com</a><br><a href= > </a><br>
 
I realy like the one line asnwer, think I will borrow the idea, however I believe the problem is to get rid of ALL text in files starting with ^/* AND ending with */& not just any line starting /* and any ending */. <p>Ged Jones<br><a href=mailto:gedejones@hotmail.com>gedejones@hotmail.com</a><br><a href= > </a><br>
 
Silly me again: -<br>sed 's/\/\/.*//' &lt;oldfile&gt;newfile<br><br>It's hard to think when collegues keep pestering you isn't it.<br><br><br> <p>Ged Jones<br><a href=mailto:gedejones@hotmail.com>gedejones@hotmail.com</a><br><a href= > </a><br>
 
This is not applicable if the content of file is as followed:-<br><br>/*<br>this is a test<br>delete this row<br>*/<br><br>or <br>/********************************<br>this is a test<br>delete this row<br>*********************************/<br><br>It will give result as followed:-<br><br>this is a test<br>delete this row<br><br>Any idea?
 
The following checks if thefirst line starts /*, if so and it ends */ then it is emptied but you could replace &gt; with rm.<br><br><br>head -1 oldfile ¦ grep &quot;^\/\*&quot; &gt; testfile <br>if [ -s testfile ] <br>then <br>tail -1 oldfile ¦ grep &quot;\*\/$&quot; &gt; testfile<br>if [ -s testfile ] <br>then <br>&gt; oldfile <br>echo &quot;file initialised&quot; <br>else <br>echo &quot;file end ok&quot; <br>fi <br>else <br>echo &quot;file start ok&quot; <br>fi<br><br>Note: About the sed option, loos like you missed a \/&nbsp;&nbsp;<br>s/&nbsp;&nbsp;&nbsp;&nbsp;: - s substitutes one string with another,<br>\/\/&nbsp;&nbsp;: - \ tells the shell not to interpret what follows<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;so in this instance it looks for //<br>.*&nbsp;&nbsp;&nbsp;&nbsp;: - wild card, means all subsequent charecters&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;will be used.<br>//&nbsp;&nbsp;&nbsp;&nbsp;:- This sets the replacement string, as none was<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;entered then the 1st string is deleted.<br><br>I think this need to be incorporated in the script like this: -<br><br>head -1 oldfile ¦ grep &quot;^\/\*&quot; &gt; testfile <br>if [ -s testfile ] <br>then <br>tail -1 oldfile ¦ grep &quot;\*\/$&quot; &gt; testfile<br>if [ -s testfile ] <br>then <br>&gt; oldfile&nbsp;&nbsp;&nbsp;# or touch oldfile or rm oldfile etc..<br>echo &quot;file contents deleted&quot; <br>else <br>echo &quot;file end ok&quot;<br>sed '/\/\/.*/d' &lt;oldfile&gt;newfile <br>fi <br>else <br>echo &quot;file start ok&quot;<br>sed '/\/\/.*/d' &lt;oldfile&gt;newfile <br>fi<br><br><br><br> <p>Ged Jones<br><a href=mailto:gedejones@hotmail.com>gedejones@hotmail.com</a><br><a href= > </a><br>
 
Ok, You guys seem to like this sorta thing, so I'll give you one you can answer for me.<br><br>History:<br><br>I am trying to write a script for some AIX 4.3.2 OS RS/6000's.&nbsp;&nbsp;I am using the generic &quot;machine_info&quot; script to gather the info.&nbsp;&nbsp;It basically does lsvg, lslv, lspv, lslpp etc.. and writes it to a file you specify.&nbsp;&nbsp;What I'm trying to do is break down each level of the script into different files for each (i.e., software inventory = lslpp -l &gt; software.xls)&nbsp;&nbsp;I put them in *.xls files and delimit them with semi-colons (;) then I can put them in a microsludge Excel spreadsheet or an Access Db table for my DR inventory.&nbsp;&nbsp;The problem I'm having is there is a section where I do &quot;lspv -l hdisk#&quot; to list physical volumes by disk.&nbsp;&nbsp;The output rendered looks like:<br><br>hdisk14:<br>LV NAME&nbsp;&nbsp;LPs PPs&nbsp;&nbsp;DISTRIBUTION&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Mount point<br>enbkup01&nbsp;&nbsp;542 542&nbsp;&nbsp;&nbsp;109..108..108..108..109/edbackup<br><br>where the &quot;109/edbackup&quot; run together, I'm trying to delimit with a (;), but the number is not always &quot;9&quot; and the letter isn't always &quot;e&quot;.&nbsp;&nbsp;How can I put a delimiter in there using a generic integer followed by a slash followed by a generic letter?&nbsp;&nbsp;I've done this with a sed statement but I have to change the statement for each different iteration of the same problem.&nbsp;&nbsp;I don't know perl well enough to figure this out, although I've learned quite a bit trying to get a solution.&nbsp;&nbsp;Any help would be greatly appreciated.<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Jon <p>Jon Zimmer<br><a href=mailto:b0rg@pcgeek.net>b0rg@pcgeek.net</a><br><a href= > </a><br>You WILL be assimilated, or we can write a script to parse you out.
 
Try a head, a tail command and 2 (two) <b>cut</b> commands:<br><FONT FACE=monospace><br>AllOutput=$(lspv -l hdisk #¦ <b>head -1</b>)<br>Header=$(echo $AllOutput¦head -1)<br>Body=$(echo $AllOutput¦tail +2)<br>FirstPartOfBody=$(echo $Body¦<b>cut -f1 -d/</b>)<br>SecondPartOfBody=$(echo $Body¦<b>cut -f2- -d/</b>)<br></font><br><br>Now you have All the data <i>before</i> the <b><font color=red>/</font></b> in <i>FirstPartOfBody</i> and all the data <i>after</i> the / in <i>SecondPartOfBody</i>...<br><br>I hope it works...
 
Since the first slash of the mountpoint is the first slash of the output line, a simple:<br><br>sed 's/\/;\//' <br><br>should work.
 
I have question about sed. If I have a file and I want to delete all lines in the file except the lines that start with the word &quot;HELLO&quot;. How should I make a script using sed command? [sig][/sig]
 
hi
I have a big problem,maybe it's really easy for you .I have a data like :

37
94
176
209
262
320
357
375
421

there is two blanks before each line .I want to put
kill -9 before each line but i don't know how to do?
Could you please help me?

Regards
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top