wb9688 Asked: 2015-06-13 06:58:14 +0800 CST2015-06-13 06:58:14 +0800 CST 2015-06-13 06:58:14 +0800 CST How do I remove comments of a file? 772 I have an file (hosts.txt) and many lines in that file are comments. How do I remove that comments? text-processing 5 Answers Voted Best Answer Mayur Kulkarni 2015-06-13T07:03:29+08:002015-06-13T07:03:29+08:00 sed '/^\#/d' myFile > tt mv tt myFile What happens here: 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. Bilal 2015-06-13T07:10:44+08:002015-06-13T07:10:44+08:00 You can use the sed Command and redirect the result to a new file by typing: sed '/^\#/d' hosts.txt > cleaned.txt A.B. 2015-06-13T07:12:52+08:002015-06-13T07:12:52+08:00 You can use -i to edit files in place. sed -i '/^#/d' hosts.txt Or with a backup sed -ibak '/^#/d' hosts.txt from man sed -i[SUFFIX], --in-place[=SUFFIX] edit files in place (makes backup if SUFFIX supplied) Maythux 2015-06-13T07:19:19+08:002015-06-13T07:19:19+08:00 you can use this: sed '/^#/ d' < inputFile.txt > outputFile.txt Zombo 2016-04-17T12:01:02+08:002016-04-17T12:01:02+08:00 You can use Vim in Ex mode: ex -sc g/^#/d -cx hosts.txt g global search d delete x save and close
What happens here:
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.
You can use the sed Command and redirect the result to a new file by typing:
You can use
-i
to edit files in place.Or with a backup
from
man sed
you can use this:
You can use Vim in Ex mode:
g
global searchd
deletex
save and close