<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: search and replace in Operating System - Linux</title>
    <link>https://community.hpe.com/t5/operating-system-linux/search-and-replace/m-p/4100283#M92578</link>
    <description>&lt;!--!*#--&gt;Hi,&lt;BR /&gt;&lt;BR /&gt;I cannot hold on me and must add some comment to the solution Nitin Kumar Gupta gave.&lt;BR /&gt;&lt;BR /&gt;@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.&lt;BR /&gt;&lt;BR /&gt;1)  You should place the output redirection out of the loop, if possible:&lt;BR /&gt;- only one open() is done instead of the number_of_lines_in_input&lt;BR /&gt;- you need no cleanup of the outfile, if it exists already&lt;BR /&gt;- you need no appending &amp;gt;&amp;gt;'&lt;BR /&gt;&lt;BR /&gt;while read line&lt;BR /&gt;do&lt;BR /&gt; ...&lt;BR /&gt;  print ...&lt;BR /&gt; ...&lt;BR /&gt;  print ...&lt;BR /&gt;done &lt;INPUTFILE&gt;/tmp/outfile&lt;BR /&gt;&lt;BR /&gt;2) Look on  l=`echo $line | grep 'snmpv2s' | wc -l`&lt;BR /&gt;- you can drop the pipe to wc, since grep is able to count:&lt;BR /&gt;l=`echo $line | grep -c 'snmpv2s'`&lt;BR /&gt;- 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.&lt;BR /&gt;- 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&lt;BR /&gt;&lt;BR /&gt;while read line&lt;BR /&gt;do &lt;BR /&gt;  if echo $line | fgrep 'snmpv2s' &amp;gt;/dev/null&lt;BR /&gt;  then print "$line&lt;BR /&gt;  else print "#$line"&lt;BR /&gt;  fi&lt;BR /&gt;done &lt;INPUTFILE&gt;/tmp/outfile&lt;BR /&gt;&lt;BR /&gt;3) You can even drop the last call to an external program, in using a 'case' statement for matching your string:&lt;BR /&gt;&lt;BR /&gt;while read line&lt;BR /&gt;do&lt;BR /&gt;  case $line in&lt;BR /&gt;  *snmpv2s*) print "$line" ;;&lt;BR /&gt;  *) print "#$line" ;;&lt;BR /&gt;  esac&lt;BR /&gt;done &lt;INPUTFILE&gt;/tmp/outfile&lt;BR /&gt;&lt;BR /&gt;Perhaps even a check against a 1MB-file shows  some runtime differences ...&lt;BR /&gt;&lt;BR /&gt;mfG Peter&lt;BR /&gt;&lt;/INPUTFILE&gt;&lt;/INPUTFILE&gt;&lt;/INPUTFILE&gt;</description>
    <pubDate>Tue, 27 Nov 2007 11:55:50 GMT</pubDate>
    <dc:creator>Peter Nikitka</dc:creator>
    <dc:date>2007-11-27T11:55:50Z</dc:date>
    <item>
      <title>search and replace</title>
      <link>https://community.hpe.com/t5/operating-system-linux/search-and-replace/m-p/4100276#M92571</link>
      <description>Hi All&lt;BR /&gt;&lt;BR /&gt;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.&lt;BR /&gt;&lt;BR /&gt;For example.I have&lt;BR /&gt;&lt;BR /&gt;snmpv2s        7878&lt;BR /&gt;snmpv2s-trap   4132 &lt;BR /&gt;&lt;BR /&gt;in the file then I wanted to comment these two lines in the file and after the script it should be&lt;BR /&gt;#snmpv2s       7878&lt;BR /&gt;#snmpv2s-trap   4312&lt;BR /&gt;&lt;BR /&gt;Thanks&lt;BR /&gt;Padma&lt;BR /&gt;</description>
      <pubDate>Sat, 10 Nov 2007 10:30:50 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/search-and-replace/m-p/4100276#M92571</guid>
      <dc:creator>Padma Asrani</dc:creator>
      <dc:date>2007-11-10T10:30:50Z</dc:date>
    </item>
    <item>
      <title>Re: search and replace</title>
      <link>https://community.hpe.com/t5/operating-system-linux/search-and-replace/m-p/4100277#M92572</link>
      <description>Hi Padma:&lt;BR /&gt;&lt;BR /&gt;# perl -pi.old -e 's/^(\s*)(snmpv2)/$1#$2/' file&lt;BR /&gt;&lt;BR /&gt;...will replace a string "snmpv2" with "#snmpv2" as long as it is the first non-whitespace sequence on a line.&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Sat, 10 Nov 2007 10:56:07 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/search-and-replace/m-p/4100277#M92572</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2007-11-10T10:56:07Z</dc:date>
    </item>
    <item>
      <title>Re: search and replace</title>
      <link>https://community.hpe.com/t5/operating-system-linux/search-and-replace/m-p/4100278#M92573</link>
      <description>The simple answer to your exact question is:just edit the file with the editor of your choice!&lt;BR /&gt;&lt;BR /&gt;However, we suspect there is much to this puzzle you are not sharing!&lt;BR /&gt;&lt;BR /&gt;For example:&lt;BR /&gt;- 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.&lt;BR /&gt;- where does the 'particular string' come from?&lt;BR /&gt;- should any match trigger the edit, or only when it is the first word?&lt;BR /&gt;&lt;BR /&gt;Anyway, maybe this is what you are looking for:&lt;BR /&gt;&lt;BR /&gt;$ perl -pe 'print q(#) if /^\s*snmpv2/' x    &lt;BR /&gt;&lt;BR /&gt;That prints a # before printing a like starting with optional spaces and the string 'snmpv2".&lt;BR /&gt;&lt;BR /&gt;Or this...&lt;BR /&gt;&lt;BR /&gt;$ my_string="snmpv2"                           &lt;BR /&gt;$ perl -i.old -pe "print q(#) if /^\s*${my_string}/" x&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Same principle, but it stick the search string in a shell vasriable and replaces that in the line.&lt;BR /&gt;It also uses the perl -i for 'in place' file update with a backup file in x.old&lt;BR /&gt;&lt;BR /&gt;If this does nto solve teh problem, then please clarify the requirements!&lt;BR /&gt;&lt;BR /&gt;Enjoy&lt;BR /&gt;Hein.&lt;BR /&gt;</description>
      <pubDate>Sat, 10 Nov 2007 11:01:34 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/search-and-replace/m-p/4100278#M92573</guid>
      <dc:creator>Hein van den Heuvel</dc:creator>
      <dc:date>2007-11-10T11:01:34Z</dc:date>
    </item>
    <item>
      <title>Re: search and replace</title>
      <link>https://community.hpe.com/t5/operating-system-linux/search-and-replace/m-p/4100279#M92574</link>
      <description>JRF, "great minds..." Hein.&lt;BR /&gt;:-)&lt;BR /&gt;</description>
      <pubDate>Sat, 10 Nov 2007 11:04:29 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/search-and-replace/m-p/4100279#M92574</guid>
      <dc:creator>Hein van den Heuvel</dc:creator>
      <dc:date>2007-11-10T11:04:29Z</dc:date>
    </item>
    <item>
      <title>Re: search and replace</title>
      <link>https://community.hpe.com/t5/operating-system-linux/search-and-replace/m-p/4100280#M92575</link>
      <description>Hi:&lt;BR /&gt;&lt;BR /&gt;&amp;gt;Hein: JRF, "great minds..." Hein. :-)&lt;BR /&gt;&lt;BR /&gt;Yes, indeed when I read your post I chuckled too!  Warmest regards, Hein, my friend!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Sat, 10 Nov 2007 13:30:55 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/search-and-replace/m-p/4100280#M92575</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2007-11-10T13:30:55Z</dc:date>
    </item>
    <item>
      <title>Re: search and replace</title>
      <link>https://community.hpe.com/t5/operating-system-linux/search-and-replace/m-p/4100281#M92576</link>
      <description># ex -s +'%s/^\(snmpv2\)/#\1/ | wq' file</description>
      <pubDate>Sat, 10 Nov 2007 15:32:31 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/search-and-replace/m-p/4100281#M92576</guid>
      <dc:creator>Sandman!</dc:creator>
      <dc:date>2007-11-10T15:32:31Z</dc:date>
    </item>
    <item>
      <title>Re: search and replace</title>
      <link>https://community.hpe.com/t5/operating-system-linux/search-and-replace/m-p/4100282#M92577</link>
      <description>If you want to comment all the line which contains the word, you can use the script:&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;while read line&lt;BR /&gt;do&lt;BR /&gt;    l=`echo $line | grep 'snmpv2s' | wc -l`&lt;BR /&gt;    if [ $l -ne 1 ]&lt;BR /&gt;    then&lt;BR /&gt;        echo "$line" &amp;gt;&amp;gt; /tmp/outfile&lt;BR /&gt;    else&lt;BR /&gt;        echo "#$line" &amp;gt;&amp;gt; /tmp/outfile&lt;BR /&gt;    fi        &lt;BR /&gt;done&lt;INPUTFILE&gt;&lt;/INPUTFILE&gt;&lt;BR /&gt;mv /tmp/outfile inputfile</description>
      <pubDate>Tue, 27 Nov 2007 08:36:51 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/search-and-replace/m-p/4100282#M92577</guid>
      <dc:creator>Nitin Kumar Gupta</dc:creator>
      <dc:date>2007-11-27T08:36:51Z</dc:date>
    </item>
    <item>
      <title>Re: search and replace</title>
      <link>https://community.hpe.com/t5/operating-system-linux/search-and-replace/m-p/4100283#M92578</link>
      <description>&lt;!--!*#--&gt;Hi,&lt;BR /&gt;&lt;BR /&gt;I cannot hold on me and must add some comment to the solution Nitin Kumar Gupta gave.&lt;BR /&gt;&lt;BR /&gt;@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.&lt;BR /&gt;&lt;BR /&gt;1)  You should place the output redirection out of the loop, if possible:&lt;BR /&gt;- only one open() is done instead of the number_of_lines_in_input&lt;BR /&gt;- you need no cleanup of the outfile, if it exists already&lt;BR /&gt;- you need no appending &amp;gt;&amp;gt;'&lt;BR /&gt;&lt;BR /&gt;while read line&lt;BR /&gt;do&lt;BR /&gt; ...&lt;BR /&gt;  print ...&lt;BR /&gt; ...&lt;BR /&gt;  print ...&lt;BR /&gt;done &lt;INPUTFILE&gt;/tmp/outfile&lt;BR /&gt;&lt;BR /&gt;2) Look on  l=`echo $line | grep 'snmpv2s' | wc -l`&lt;BR /&gt;- you can drop the pipe to wc, since grep is able to count:&lt;BR /&gt;l=`echo $line | grep -c 'snmpv2s'`&lt;BR /&gt;- 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.&lt;BR /&gt;- 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&lt;BR /&gt;&lt;BR /&gt;while read line&lt;BR /&gt;do &lt;BR /&gt;  if echo $line | fgrep 'snmpv2s' &amp;gt;/dev/null&lt;BR /&gt;  then print "$line&lt;BR /&gt;  else print "#$line"&lt;BR /&gt;  fi&lt;BR /&gt;done &lt;INPUTFILE&gt;/tmp/outfile&lt;BR /&gt;&lt;BR /&gt;3) You can even drop the last call to an external program, in using a 'case' statement for matching your string:&lt;BR /&gt;&lt;BR /&gt;while read line&lt;BR /&gt;do&lt;BR /&gt;  case $line in&lt;BR /&gt;  *snmpv2s*) print "$line" ;;&lt;BR /&gt;  *) print "#$line" ;;&lt;BR /&gt;  esac&lt;BR /&gt;done &lt;INPUTFILE&gt;/tmp/outfile&lt;BR /&gt;&lt;BR /&gt;Perhaps even a check against a 1MB-file shows  some runtime differences ...&lt;BR /&gt;&lt;BR /&gt;mfG Peter&lt;BR /&gt;&lt;/INPUTFILE&gt;&lt;/INPUTFILE&gt;&lt;/INPUTFILE&gt;</description>
      <pubDate>Tue, 27 Nov 2007 11:55:50 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/search-and-replace/m-p/4100283#M92578</guid>
      <dc:creator>Peter Nikitka</dc:creator>
      <dc:date>2007-11-27T11:55:50Z</dc:date>
    </item>
    <item>
      <title>Re: search and replace</title>
      <link>https://community.hpe.com/t5/operating-system-linux/search-and-replace/m-p/4100284#M92579</link>
      <description>&amp;gt;Peter: Since (f)grep gives exactly that as a return value ...&lt;BR /&gt;  if echo $line | fgrep 'snmpv2s' &amp;gt;/dev/null&lt;BR /&gt;&lt;BR /&gt;For improvement 2.5:  :-)&lt;BR /&gt;With grep -q, you can also drop the output redirection.</description>
      <pubDate>Wed, 28 Nov 2007 04:56:52 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/search-and-replace/m-p/4100284#M92579</guid>
      <dc:creator>Dennis Handly</dc:creator>
      <dc:date>2007-11-28T04:56:52Z</dc:date>
    </item>
    <item>
      <title>Re: search and replace</title>
      <link>https://community.hpe.com/t5/operating-system-linux/search-and-replace/m-p/4100285#M92580</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;@Dennis:&lt;BR /&gt;As another improvement '2.5a' one could use the  fgrep output instead of supressing it, since fgrep will do already the desired output:&lt;BR /&gt;&lt;BR /&gt;while read line&lt;BR /&gt;do&lt;BR /&gt;  if echo $line | fgrep 'snmpv2s'&lt;BR /&gt;  then :&lt;BR /&gt;  else print "#$line"&lt;BR /&gt;  fi&lt;BR /&gt;done &lt;INPUTFILE&gt;/tmp/outfile&lt;BR /&gt;&lt;BR /&gt;mfG Peter,&lt;BR /&gt;who recommends still improvement 3.&lt;/INPUTFILE&gt;</description>
      <pubDate>Wed, 28 Nov 2007 05:43:34 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/search-and-replace/m-p/4100285#M92580</guid>
      <dc:creator>Peter Nikitka</dc:creator>
      <dc:date>2007-11-28T05:43:34Z</dc:date>
    </item>
  </channel>
</rss>

