1820530 Members
2250 Online
109626 Solutions
New Discussion юеВ

search and replace

 
SOLVED
Go to solution
Padma Asrani
Honored Contributor

search and replace

Hi All

I am looking for some help here. Basically I wanted to search for a particular string within a file and put the # starting that string.

For example.I have

snmpv2s 7878
snmpv2s-trap 4132

in the file then I wanted to comment these two lines in the file and after the script it should be
#snmpv2s 7878
#snmpv2s-trap 4312

Thanks
Padma
9 REPLIES 9
James R. Ferguson
Acclaimed Contributor

Re: search and replace

Hi Padma:

# perl -pi.old -e 's/^(\s*)(snmpv2)/$1#$2/' file

...will replace a string "snmpv2" with "#snmpv2" as long as it is the first non-whitespace sequence on a line.

Regards!

...JRF...
Hein van den Heuvel
Honored Contributor

Re: search and replace

The simple answer to your exact question is:just edit the file with the editor of your choice!

However, we suspect there is much to this puzzle you are not sharing!

For example:
- why should it be in a script / procedural form? That suggest an automation requirement of input/output for which the params are not presented here.
- where does the 'particular string' come from?
- should any match trigger the edit, or only when it is the first word?

Anyway, maybe this is what you are looking for:

$ perl -pe 'print q(#) if /^\s*snmpv2/' x

That prints a # before printing a like starting with optional spaces and the string 'snmpv2".

Or this...

$ my_string="snmpv2"
$ perl -i.old -pe "print q(#) if /^\s*${my_string}/" x


Same principle, but it stick the search string in a shell vasriable and replaces that in the line.
It also uses the perl -i for 'in place' file update with a backup file in x.old

If this does nto solve teh problem, then please clarify the requirements!

Enjoy
Hein.
Hein van den Heuvel
Honored Contributor

Re: search and replace

JRF, "great minds..." Hein.
:-)
James R. Ferguson
Acclaimed Contributor

Re: search and replace

Hi:

>Hein: JRF, "great minds..." Hein. :-)

Yes, indeed when I read your post I chuckled too! Warmest regards, Hein, my friend!

...JRF...
Sandman!
Honored Contributor
Solution

Re: search and replace

# ex -s +'%s/^\(snmpv2\)/#\1/ | wq' file
Nitin Kumar Gupta
Trusted Contributor

Re: search and replace

If you want to comment all the line which contains the word, you can use the script:


while read line
do
l=`echo $line | grep 'snmpv2s' | wc -l`
if [ $l -ne 1 ]
then
echo "$line" >> /tmp/outfile
else
echo "#$line" >> /tmp/outfile
fi
done
mv /tmp/outfile inputfile
Peter Nikitka
Honored Contributor

Re: search and replace

Hi,

I cannot hold on me and must add some comment to the solution Nitin Kumar Gupta gave.

@Nitin: don't panic - your commands will work in most cases. Though perl, awk may be shorter, it is perfectly legal to do it all at shellscript level.

1) You should place the output redirection out of the loop, if possible:
- only one open() is done instead of the number_of_lines_in_input
- you need no cleanup of the outfile, if it exists already
- you need no appending >>'

while read line
do
...
print ...
...
print ...
done /tmp/outfile

2) Look on l=`echo $line | grep 'snmpv2s' | wc -l`
- you can drop the pipe to wc, since grep is able to count:
l=`echo $line | grep -c 'snmpv2s'`
- you do not want to match a pattern but a string, so you can use the possible faster 'fgrep', which looks for plain strings only.
- there is no real use for the count of matches: you only need true or false. Since (f)grep gives exactly that as a return value, you can drop the compare-statement. So we have till now

while read line
do
if echo $line | fgrep 'snmpv2s' >/dev/null
then print "$line
else print "#$line"
fi
done /tmp/outfile

3) You can even drop the last call to an external program, in using a 'case' statement for matching your string:

while read line
do
case $line in
*snmpv2s*) print "$line" ;;
*) print "#$line" ;;
esac
done /tmp/outfile

Perhaps even a check against a 1MB-file shows some runtime differences ...

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Dennis Handly
Acclaimed Contributor

Re: search and replace

>Peter: Since (f)grep gives exactly that as a return value ...
if echo $line | fgrep 'snmpv2s' >/dev/null

For improvement 2.5: :-)
With grep -q, you can also drop the output redirection.
Peter Nikitka
Honored Contributor

Re: search and replace

Hi,

@Dennis:
As another improvement '2.5a' one could use the fgrep output instead of supressing it, since fgrep will do already the desired output:

while read line
do
if echo $line | fgrep 'snmpv2s'
then :
else print "#$line"
fi
done /tmp/outfile

mfG Peter,
who recommends still improvement 3.
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"