I have text files with tons of comments, how do I get rid of those in one second?
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
nfirst lines.
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' myFileremoves all lines starting with#from the filemyFileand outputs the result in the console,> ttredirects the output into a temporary file calledtt,mv tt myFilemoves the temporary filetttomyFile.
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
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...

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?