Re: Text file editing - remove trailing comments?



In alt.os.linux.slackware Mike Jones <luck@xxxxxxxxxxxxxxx> wrote:


I have a long file with more lines than I fancy manually editing, and
need to cut all the added comments from any lines that have them.

ie:

line1
line2 # comments
line3
line4 # with notes
line5 # more stuff

...becomes

line1
line2
line3
line4
line5

Anybody got quick'n'simple technique for this kind of thing?

A job for sed:
sed "s/#.*//" file > newfile
or if you want the spaces before the # also removed
sed "S/ *#.*//" file > newfile

Note: "newfile" MUST be a different filename then the inputfile,
otherwise the inputfile gets zero'd before being opened.
It is possible to edit the file directly in-place with the option
--in-place=bak
then the original file will be backup'ed first and then edited
in-place, so then the output redirection isn't needed.
--
******************************************************************
** Eef Hartman, Delft University of Technology, dept. SSC/ICT **
** e-mail: E.J.M.Hartman@xxxxxxxxxx - phone: +31-15-27 82525 **
******************************************************************
.