System Administration
cancel
Showing results for 
Search instead for 
Did you mean: 

insert # in the begining of a line in which the required word exists.

 
SOLVED
Go to solution
Vinoyee Madashery Poulo
Frequent Advisor

insert # in the begining of a line in which the required word exists.


Hi Experts,

It is required to insert # in the begining of a line in which the a particular word exists. I don't want to create a new file then replace the original file.

For example, I just want to comment the following line in /etc/fstab
/dev/vg00/lvol7 /usr vxfs delaylog 0 2
Using sed or any other command can I acheive this?
Creating a new file and copying it back is possible. Is there any other method?

9 REPLIES
Dennis Handly
Acclaimed Contributor

Re: insert # in the begining of a line in which the required word exists.

You can use sed:
sed '\:/dev/vg00/lvol7:s/^/#/' /etc/fstab > /etc/fstab.new
Fredrik.eriksson
Valued Contributor

Re: insert # in the begining of a line in which the required word exists.

You can use the -i syntax for sed to replace the pattern directly in the file, but I don't recommend doing this unless you already backed up your fstab file.

sed -i "s/^\(\/dev\/vg00\/lvol7.*\)$/#\1/" /etc/fstab

where \/dev\/vg00\/lvol7 is the search pattern.
Remember to escape / since sed see it as a delimiter between search and replace patterns.

Best regards
Fredrik Eriksson
Suraj K Sankari
Honored Contributor

Re: insert # in the begining of a line in which the required word exists.

hi,

Its a good practics to make a backup of file.
cp -p /etc/fstab /etc/fstab.org
awk '{print "#"$0 }'/etc/fstab

using sed
please see the examples given in below link
http://sed.sourceforge.net/sed1line.txt

Suraj
Vinoyee Madashery Poulo
Frequent Advisor

Re: insert # in the begining of a line in which the required word exists.

Dennis,

I don't like to create a new file. I would like to make changes in the file itself.
Vinoyee Madashery Poulo
Frequent Advisor

Re: insert # in the begining of a line in which the required word exists.

Fredrik,

I get error " sed: illegal option -- i". OS is Hp-UX 11.23
Dennis Handly
Acclaimed Contributor
Solution

Re: insert # in the begining of a line in which the required word exists.

>I don't like to create a new file. I would like to make changes in the file itself.

I suppose you could use ex:
ex <g:/dev/vg00/lvol7:s/^/#/
wq
EOF

>Fredrik: You can use the -i syntax for sed

This isn't valid for HP-UX's sed.
T G Manikandan
Honored Contributor

Re: insert # in the begining of a line in which the required word exists.

find /etc -name fstab | xargs perl -pi -e 's/^\/dev\/cdrom/#\/dev\/cdrom/'
Fredrik.eriksson
Valued Contributor

Re: insert # in the begining of a line in which the required word exists.

Dennis, oh that's entirely correct... thought I was writing in the linux forum *ashamed*. This is why you shouldn't start thinking when you haven't even had your first cup of coffee :P

I would try out one of the awk or perl hacks that was written above.

please assign 0 points for this one :)

Best regards
Fredrik Eriksson
Vinoyee Madashery Poulo
Frequent Advisor

Re: insert # in the begining of a line in which the required word exists.

Thanks for the response.

Dennis,
Your answer is met my expectation. It worked fine.

Fredrik: Assigned 2 points for immediate acknowledgement of mistakes.