Operating System - HP-UX
1848990 Members
7511 Online
104040 Solutions
New Discussion

Re: Remove a comment # from a file

 
SOLVED
Go to solution
Andrew_80
Advisor

Remove a comment # from a file

Any one know How to remove a "#" from a file without using vi (using script).
I think I am half way the problem as I believe I should use ch_rc command, but I am not sure of the syntax.

Thanks
The Sky is the Limit
18 REPLIES 18
Pete Randall
Outstanding Contributor

Re: Remove a comment # from a file

Well, there's always sed:

sed 's|#AnyOtherTextNecessaryToIdentifyTheLine||g'`



Pete


Pete
Sergejs Svitnevs
Honored Contributor

Re: Remove a comment # from a file

sed 's/#//g' file > new_file

Regards,
Sergejs
Andrew_80
Advisor

Re: Remove a comment # from a file

This is OK, But I need to edit a file like
"/etc/rc.config.d/savecrash" and remove the comments from three lines and the sed allow you to only create it on anew file. So, I am trying to save multiple steps.

Thanks
The Sky is the Limit
Bruno Ganino
Honored Contributor

Re: Remove a comment # from a file

...more
sed 's/#//n' infile > outfile
(n=1 to 512). Substitute for just the n'th occurrence.
Bruno
Torino (Turin) +2H
Michael Steele_2
Honored Contributor

Re: Remove a comment # from a file

grep -v \# file1 > file2
Support Fatherhood - Stop Family Law
Bruno Ganino
Honored Contributor

Re: Remove a comment # from a file

see also command ed
Torino (Turin) +2H
Paddy_1
Valued Contributor

Re: Remove a comment # from a file

Use this perl script to display the file and redirect it as neccesary
-----------------------------

open I, $ARGV[0] or die "cannot open $ARGV[0]: $!";

while ($line=)
{
next if $line=~ /^#/;
print $line;
}
------------------------
The sufficiency of my merit is to know that my merit is NOT sufficient
Andrew_80
Advisor

Re: Remove a comment # from a file

Remember I just want to remove the '#' from some specific lines not the whole file savecrash.

# SAVECRASH=1
# SAVECRASH_DIR=/var/adm/crash
# COMPRESS=2

I know how to change the values using

#ch_rc -a -p

and on the man pages it says you can remove characters also, but I am not sure How ?
I appreciate your responses.

Thanks
The Sky is the Limit
Mark Grant
Honored Contributor
Solution

Re: Remove a comment # from a file

mv savecrash savecrash.prev
cat savecrash.prev | sed s/^#//g > savecrash
Never preceed any demonstration with anything more predictive than "watch this"
Andrew_80
Advisor

Re: Remove a comment # from a file

But that will remove it from all the lines including text !!
The Sky is the Limit
harry d brown jr
Honored Contributor

Re: Remove a comment # from a file


You need to use "vi", "emacs" or "ed". Unless you are a pattern matching expert and know how to write code in perl, your request for another "answer", other than editors, is highly unlikely, especially since you want to have an answer that magically knows which "#" lines to remove or possibly "un-comment" lines and which not to.

If you don't like "vi", then copy the progam to your desktop and use "wordpad".

live free or die
harry
Live Free or Die
Robert-Jan Goossens
Honored Contributor

Re: Remove a comment # from a file

Why do'nt you want to use vi ??
Steven Sim Kok Leong
Honored Contributor

Re: Remove a comment # from a file

Hi,

I tend to agree with Harry.

In addition, if you just want to get rid of consecutive lines at the beginning or at the end of the file, you can use head and/or tail. For instance, you want the 4th line onwards from the file, then:

# tail +4 myfile > myfile2

Hope this helps. Regards.

Steven Sim Kok Leong
Mark Grant
Honored Contributor

Re: Remove a comment # from a file

My last post only removes the "#" not the text. It does do it for all lines though. There is no way that you can be sure you are uncommenting the right thing unless you give the script something to match against. Now that is going to start to be annoying when uncommenting large files.

If it is something you find you are doing a lot of just make a copy of the file with the comments in and another with the comments out and just cp the one you want when you want.
Never preceed any demonstration with anything more predictive than "watch this"
Andrew_80
Advisor

Re: Remove a comment # from a file

Thanks a lot, The reason I don't want to use vi is that I have to do the same thing across 20 machines !

I will use Mark's solution
cat savecrash.prev | sed s/^#//g > savecrash
and specify each line.

Thanks again
The Sky is the Limit
Jean-Luc Oudart
Honored Contributor

Re: Remove a comment # from a file

If you know which line pattern you want to replace say # SAVECRASH
then you can use vi
this way
create "mycom" a command file :
:1,$s/^# SAVECRASH/SAVECRASH/
:wq

then run
vi < mycom >/dev/null

Rgds,
Jean-Luc
fiat lux
Bruno Ganino
Honored Contributor

Re: Remove a comment # from a file

Well, try
cat savecrash | sed s/# SAVECRASH//g > savecrash.tmp
cat savecrash.tmp | sed s/# COMPRESS//g > savecrash

P.S. option g=globale or n=occorrence
HTH
Bruno
Torino (Turin) +2H
Bruno Ganino
Honored Contributor

Re: Remove a comment # from a file

Before not right, This is OK! Sorry
Well, try
cat savecrash | sed s/# SAVECRASH/SAVECRASH/g > savecrash.tmp
cat savecrash.tmp | sed s/# COMPRESS/COMPRESS/g > savecrash
P.S. option g=globale or n=occorrence
HTH
Bruno
Torino (Turin) +2H