- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- search and replace
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-10-2007 02:30 AM
тАО11-10-2007 02:30 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-10-2007 02:56 AM
тАО11-10-2007 02:56 AM
Re: search and replace
# 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...
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-10-2007 03:01 AM
тАО11-10-2007 03:01 AM
Re: search and replace
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-10-2007 03:04 AM
тАО11-10-2007 03:04 AM
Re: search and replace
:-)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-10-2007 05:30 AM
тАО11-10-2007 05:30 AM
Re: search and replace
>Hein: JRF, "great minds..." Hein. :-)
Yes, indeed when I read your post I chuckled too! Warmest regards, Hein, my friend!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-10-2007 07:32 AM
- Tags:
- ex
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-27-2007 12:36 AM
тАО11-27-2007 12:36 AM
Re: search and replace
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-27-2007 03:55 AM
тАО11-27-2007 03:55 AM
Re: search and replace
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
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
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
Perhaps even a check against a 1MB-file shows some runtime differences ...
mfG Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-27-2007 08:56 PM
тАО11-27-2007 08:56 PM
Re: search and replace
if echo $line | fgrep 'snmpv2s' >/dev/null
For improvement 2.5: :-)
With grep -q, you can also drop the output redirection.
- Tags:
- grep
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-27-2007 09:43 PM
тАО11-27-2007 09:43 PM
Re: search and replace
@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
mfG Peter,
who recommends still improvement 3.