ZoneO-tips for Mandriva Linux

Remove comment lines in a text file

I have text files with tons of comments, how do I get rid of those in one second?

 
 
Start the Konsole
Figure 1: Start the Konsole

This is almost a copy of a previous post (Remove empty lines with sed), but it could be handy for some of us!

Sometimes, you may want to delete all comments from a text file. Basically, there are two cases

  • you comment lines start with a given symbol,
  • you comments are on the n first lines.
Both cases have to be dealt with differently...

Deleting comment lines starting with a special symbol

Let's say that your comment lines start with the symbol #. You have a handy one-liner for sed that will get rid of them:

sed '/^\#/d' myFile > tt
mv tt myFile
Here is what happens:
  • sed '/^\#/d' myFile removes all lines starting with # from the file myFile and outputs the result in the console,
  • > tt redirects the output into a temporary file called tt,
  • mv tt myFile moves the temporary file tt to myFile.

If your comment lines start with the symbol c followed by a space, the commands become

sed '/^c\ /d' myFile > tt
mv tt myFile

And so on, you get the trick!

Deleting comment lines on top of a file

In other cases, you may have a text file for which a give number of lines on top are comments. Let's say that you want to get rid of the first 3 lines. Well, here is the trick:

sed '1,3d' myFile > tt
mv tt myFile

No big deal as you can see!

Processing multiple files...

Now, you may have 100 files to process at the same time. That's where foreach comes in... Let's say you want to correct all files ending with .dat, here is what you should do:

  • open up a konsole (Figure 1),
  • move into the directory where your files reside,
  • type the following commands:
    foreach file (*dat)
    sed '/^\#/d' $file > tt
    mv tt $file
    end
That should take care of it...
03/2006
 

shwetha, 15 April 2008

Instead of deleting the comments, i need the sed to skip scanning the comments in a text file for regular expressions. Is that possible?

response, 25 April 2008

why should you do

"sed '/^#/d' myFile > tt

mv tt myFile"

when you can simply do

"sed -i '/^#/d' myFile"

response, 25 April 2008

why should you do

"sed '/^#/d' myFile > tt

mv tt myFile"

when you can simply do

"sed -i '/^#/d' myFile"

response, 25 April 2008

shwetha, yes it is

http://www.unix.com/shell-programming-scripting/20951-ignore...

revathy, 02 July 2009

How to remove or replace a junk character which in every line of a specific row..i want remove just that junk value or character which at end of say row 6.its huge dat file.

louis vuitton, 28 October 2009

<a href=http://www.flashreplica.com>louis vuitton</a>...[/link] vuitton[/url]

lvhandbags, 24 December 2009

<A href="http://www.eluxuryin.com/" target=_blank>loui...[/link]" target=_blank>replica handbags</A>

<A href="http://www.eluxuryin.com/" target=_blank>repl...[/link]" target=_blank>eluxury</A>

<A href="http://www.eluxuryin.com/" target=_blank>loui...[/link]" target=_blank>louis vuitton bags</A>

<A href="http://www.eluxuryin.com/" target=_blank>cheap louis vuitton</A>

vQEllen, 03 February 2010

Thank you for the article just about this good topic! People know that the essay writing organization will propose the <a href=" http://www.exclusivepapers.com">essay writing...[/link]">custom essays</a> about this post.

Add a comment

Name:
Email or URL: (optional)
Security code
Code shown above:
 
 
© ZoneO-soft - Contact us - Start page