<?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 script in Operating System - Linux</title>
    <link>https://community.hpe.com/t5/operating-system-linux/search-and-replace-script/m-p/4986144#M100044</link>
    <description>Hi Sally,&lt;BR /&gt;&lt;BR /&gt;Sorry I had left office so couldnt reply.&lt;BR /&gt;Is your query been resolved ? Or you still need any other way. Do let us know, so that only if you still need something , can work out on a resolution.&lt;BR /&gt;&lt;BR /&gt;Regards,&lt;BR /&gt;Ninad</description>
    <pubDate>Tue, 20 Jun 2006 03:13:21 GMT</pubDate>
    <dc:creator>Ninad_1</dc:creator>
    <dc:date>2006-06-20T03:13:21Z</dc:date>
    <item>
      <title>Search and replace script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/search-and-replace-script/m-p/4986136#M100036</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;The /etc/hosts files are going to be changing on all our servers soon as all our IP address are changing.  Each hosts file is different.  I wonder if it is possible to create a script that will take each host and verify the current IP with an nslookup - if it matches, leave it - but if not, replace with the new address.&lt;BR /&gt;&lt;BR /&gt;Thanks,&lt;BR /&gt;S.</description>
      <pubDate>Mon, 19 Jun 2006 09:58:34 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/search-and-replace-script/m-p/4986136#M100036</guid>
      <dc:creator>Coolmar</dc:creator>
      <dc:date>2006-06-19T09:58:34Z</dc:date>
    </item>
    <item>
      <title>Re: Search and replace script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/search-and-replace-script/m-p/4986137#M100037</link>
      <description>Hi Sally,&lt;BR /&gt;I would be tempted to make up a good "master" /etc/hosts that I know is right and overwrite the ones out in the field. &lt;BR /&gt;Do yuo have servers that are known by different names by other machines?&lt;BR /&gt;If you are confident in the info on your name servers (assuming that you have more than one for redundancy) you could ignore the hosts file altogether and manage everything through DNS by changing your nsswitch.conf.&lt;BR /&gt;&lt;BR /&gt;regards&lt;BR /&gt;Ian</description>
      <pubDate>Mon, 19 Jun 2006 10:10:06 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/search-and-replace-script/m-p/4986137#M100037</guid>
      <dc:creator>Ian Vaughan</dc:creator>
      <dc:date>2006-06-19T10:10:06Z</dc:date>
    </item>
    <item>
      <title>Re: Search and replace script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/search-and-replace-script/m-p/4986138#M100038</link>
      <description>Shalom Sally,&lt;BR /&gt;&lt;BR /&gt;I would suggest creating a file list on each server and using sed&lt;BR /&gt;&lt;BR /&gt;filelist&lt;BR /&gt;&lt;BR /&gt;/etc/hosts&lt;BR /&gt;/etc/rc.config.d/netconf&lt;BR /&gt;# add what you need here for each server.&lt;BR /&gt;&lt;BR /&gt;The script(outline)&lt;BR /&gt;&lt;BR /&gt;while read -r filename&lt;BR /&gt;do&lt;BR /&gt; sed s/192.168.0.10/10.63.31.245/g $filename &amp;gt;filename.new&lt;BR /&gt; mv $filename.new $filename&lt;BR /&gt;done &amp;lt; filename&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;If you are careful, you can generate and edit the file list as follows&lt;BR /&gt;&lt;BR /&gt;find / -exec grep -l '192.168.0.10' {} \; &amp;gt; filelist&lt;BR /&gt;&lt;BR /&gt;edit it and get ready to run.&lt;BR /&gt;&lt;BR /&gt;This same basic concept can be used for many kinds of editing.&lt;BR /&gt;&lt;BR /&gt;SEP</description>
      <pubDate>Mon, 19 Jun 2006 10:13:12 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/search-and-replace-script/m-p/4986138#M100038</guid>
      <dc:creator>Steven E. Protter</dc:creator>
      <dc:date>2006-06-19T10:13:12Z</dc:date>
    </item>
    <item>
      <title>Re: Search and replace script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/search-and-replace-script/m-p/4986139#M100039</link>
      <description>Please use the below script&lt;BR /&gt;&lt;BR /&gt;#!/usr/bin/ksh&lt;BR /&gt;egrep -v "^#|^$" /etc/hosts | awk '{print $1,$2}' | while read ipaddr hostnm rest&lt;BR /&gt; do&lt;BR /&gt;  nslookupip=$(nslookup $hostnm 2&amp;gt;/dev/null | awk '/Trying DNS/,/zzz/' | grep -i address | awk '{print $NF}')&lt;BR /&gt;  if [[ -z $nslookupip ]]&lt;BR /&gt;   then&lt;BR /&gt;      echo blank&lt;BR /&gt;   else&lt;BR /&gt;      if [[ "$ipaddr" = "$nslookupip" ]]&lt;BR /&gt;       then&lt;BR /&gt;          echo $hostnm ip address not changed  &amp;gt;&amp;gt; newhosts.log&lt;BR /&gt;          echo $ipaddr $hostnm $rest &amp;gt;&amp;gt; newhosts&lt;BR /&gt;       else&lt;BR /&gt;          echo $hostnm Change ip to $nslookupip  &amp;gt;&amp;gt; newhosts.log&lt;BR /&gt;          echo $nslookupip $hostnm $rest &amp;gt;&amp;gt; newhosts&lt;BR /&gt;      fi&lt;BR /&gt;   fi&lt;BR /&gt;done&lt;BR /&gt;&lt;BR /&gt;The newly generated file will be newhosts and log file will be newhosts.log&lt;BR /&gt;Check before replacing the hosts file and always take a backup of the current hosts file before replacing.&lt;BR /&gt;&lt;BR /&gt;Regards,&lt;BR /&gt;Ninad</description>
      <pubDate>Mon, 19 Jun 2006 10:23:15 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/search-and-replace-script/m-p/4986139#M100039</guid>
      <dc:creator>Ninad_1</dc:creator>
      <dc:date>2006-06-19T10:23:15Z</dc:date>
    </item>
    <item>
      <title>Re: Search and replace script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/search-and-replace-script/m-p/4986140#M100040</link>
      <description>Thanks Ninad...that is a great script.  My only problem is that my nsswitch.conf (which I cannot change) has it looking at files first and then DNS second.  So in that order, your script will not work unless I can do an nslookup with a force to DNS only.&lt;BR /&gt;&lt;BR /&gt;S.</description>
      <pubDate>Mon, 19 Jun 2006 10:46:04 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/search-and-replace-script/m-p/4986140#M100040</guid>
      <dc:creator>Coolmar</dc:creator>
      <dc:date>2006-06-19T10:46:04Z</dc:date>
    </item>
    <item>
      <title>Re: Search and replace script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/search-and-replace-script/m-p/4986141#M100041</link>
      <description>The nslookup command can be given a dns server name as a second parameter.  That will force it to us DNS when /etc/nsswitch.conf says to start with /etc/hosts.</description>
      <pubDate>Mon, 19 Jun 2006 11:09:28 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/search-and-replace-script/m-p/4986141#M100041</guid>
      <dc:creator>Mike Stroyan</dc:creator>
      <dc:date>2006-06-19T11:09:28Z</dc:date>
    </item>
    <item>
      <title>Re: Search and replace script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/search-and-replace-script/m-p/4986142#M100042</link>
      <description>Hi Sally:&lt;BR /&gt;&lt;BR /&gt;Here's a simple Perl script that will verify '/etc/hosts' files.  The script will report (print) any entries where the hostname does *not* resolve to the listed IPaddress.&lt;BR /&gt;&lt;BR /&gt;For example, given an entry like:&lt;BR /&gt;&lt;BR /&gt;10.11.10.11 myserver &lt;BR /&gt;&lt;BR /&gt;This line would be printed if the number and name were not equivalent.&lt;BR /&gt;&lt;BR /&gt;# cat .hostproof&lt;BR /&gt;#!/usr/bin/perl&lt;BR /&gt;use strict;&lt;BR /&gt;use warnings;  &lt;BR /&gt;use Socket;&lt;BR /&gt;while (&amp;lt;&amp;gt;) {&lt;BR /&gt;    next if /^#/;&lt;BR /&gt;    next if /^$/;&lt;BR /&gt;    my @F = split;&lt;BR /&gt;    my $derived_ip = gethostbyname($F[1]);&lt;BR /&gt;    print if $F[0] ne inet_ntoa($derived_ip);&lt;BR /&gt;}&lt;BR /&gt;1;&lt;BR /&gt;&lt;BR /&gt;...Run as :&lt;BR /&gt;&lt;BR /&gt;# ./hostproof /etc/hosts&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Mon, 19 Jun 2006 11:12:25 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/search-and-replace-script/m-p/4986142#M100042</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2006-06-19T11:12:25Z</dc:date>
    </item>
    <item>
      <title>Re: Search and replace script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/search-and-replace-script/m-p/4986143#M100043</link>
      <description>Hi (again) Sally:&lt;BR /&gt;&lt;BR /&gt;OK, I reread your post.  The first Perl script I suggested does a verification only of an '/etc/hosts' file to report lines that require change.&lt;BR /&gt;&lt;BR /&gt;This version will recreate an 'etc/hosts' file fixing any IP addresses to match the currently derived address associated with each hostname.&lt;BR /&gt;&lt;BR /&gt;The '/etc/hosts' file is preserved as '/etc/hosts.old' and a new file is generated.&lt;BR /&gt;&lt;BR /&gt;# cat .hostfix&lt;BR /&gt;#!/usr/bin/perl -ni.old&lt;BR /&gt;use strict;&lt;BR /&gt;use warnings;  &lt;BR /&gt;use Socket;&lt;BR /&gt;/^#/    and print, next;&lt;BR /&gt;/^\s*$/ and print, next;&lt;BR /&gt;my @F = split;&lt;BR /&gt;my $packed_ip  = gethostbyname($F[1]);&lt;BR /&gt;my $derived_ip = inet_ntoa($packed_ip);&lt;BR /&gt;s/\s*\d+\.\d+\.\d+.\d+/$derived_ip/ if ($F[0] ne $derived_ip);&lt;BR /&gt;print;&lt;BR /&gt;1;&lt;BR /&gt;&lt;BR /&gt;...Run as:&lt;BR /&gt;&lt;BR /&gt;# ./hostfix /etc/hosts&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...&lt;BR /&gt;</description>
      <pubDate>Mon, 19 Jun 2006 12:51:08 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/search-and-replace-script/m-p/4986143#M100043</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2006-06-19T12:51:08Z</dc:date>
    </item>
    <item>
      <title>Re: Search and replace script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/search-and-replace-script/m-p/4986144#M100044</link>
      <description>Hi Sally,&lt;BR /&gt;&lt;BR /&gt;Sorry I had left office so couldnt reply.&lt;BR /&gt;Is your query been resolved ? Or you still need any other way. Do let us know, so that only if you still need something , can work out on a resolution.&lt;BR /&gt;&lt;BR /&gt;Regards,&lt;BR /&gt;Ninad</description>
      <pubDate>Tue, 20 Jun 2006 03:13:21 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/search-and-replace-script/m-p/4986144#M100044</guid>
      <dc:creator>Ninad_1</dc:creator>
      <dc:date>2006-06-20T03:13:21Z</dc:date>
    </item>
    <item>
      <title>Re: Search and replace script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/search-and-replace-script/m-p/4986145#M100045</link>
      <description>Hi Ninad,&lt;BR /&gt;Still fighting with it.  Our IP addresses have to change when our servers move.  I figure relying on the DNS might not be a good idea either.  Plus there are some systems that don't have DNS access.  So my new idea is to create a flat file with the new addresses of the servers that are changing.  Then scan each server for any of the addresses in the flat file and if it is there, take the new address.  So here is how I modified your script below.  The problem that I am having is that only the last entry actually gets changed.  The others do but then go back to the original value and only the last one changes.  The problem is in the sed line and to the filename.new.&lt;BR /&gt;&lt;BR /&gt;if [ `whoami` != "root" ]; then&lt;BR /&gt;        echo "This script must be run with root permissions!"&lt;BR /&gt;        exit&lt;BR /&gt;fi&lt;BR /&gt;filename=/etc/hosts&lt;BR /&gt;d=$HOME/scripts/move&lt;BR /&gt;ips=$d/ips.list&lt;BR /&gt;if [ -f $d/$filename.new ]; then&lt;BR /&gt;        rm $d/$filename.new&lt;BR /&gt;fi&lt;BR /&gt;egrep -v "^#|^$|localhost" /etc/hosts | awk '{print $1,$2}' | while read ipaddr hostnm&lt;BR /&gt;do&lt;BR /&gt;for host in `echo $hostnm |awk -F. '{print $1}'`; do&lt;BR /&gt;        #host=`echo $hostnm |awk -F. '{print $1}'`&lt;BR /&gt;        newip=`grep $host $ips |awk '{print $2}'`&lt;BR /&gt;        if [[ -z $newip ]]&lt;BR /&gt;        then&lt;BR /&gt;        echo blank&lt;BR /&gt;        else&lt;BR /&gt;                if [[ "$ipaddr" != "$newip" ]]&lt;BR /&gt;                then&lt;BR /&gt;                cp $filename $filename.orig&lt;BR /&gt;        #       sed s/$ipaddr/$newip/g $filename &amp;gt;&amp;gt;$filename.new&lt;BR /&gt;                sed s/$ipaddr/$newip/g $filename&lt;BR /&gt;        #       mv $filename.new $filename&lt;BR /&gt;                fi&lt;BR /&gt;        fi&lt;BR /&gt;done&lt;BR /&gt;done&lt;BR /&gt;</description>
      <pubDate>Tue, 20 Jun 2006 06:54:44 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/search-and-replace-script/m-p/4986145#M100045</guid>
      <dc:creator>Coolmar</dc:creator>
      <dc:date>2006-06-20T06:54:44Z</dc:date>
    </item>
    <item>
      <title>Re: Search and replace script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/search-and-replace-script/m-p/4986146#M100046</link>
      <description>OK, I think I am understanding your requirement in a better way now than I did before.&lt;BR /&gt;So you are saying that the ip addresses have not changed yet but are going to change - thus we cannot verify using either ping nor DNS.&lt;BR /&gt;So as you have found an alternative - write ip addresses to a flat file - I agree with you is the most suitable option for you.&lt;BR /&gt;Now as I understand what needs to be done is read hosts file , check ip address has changed or not from the flat file and update the entry if ip addr changed and leave as is if not changed.&lt;BR /&gt;I would propose you a simpler solution.&lt;BR /&gt;The logic is if you find entry in flat file - means ip address is changed - no need to check in hosts files again.&lt;BR /&gt;If no entry found in flat file - means ip address is unchanged.&lt;BR /&gt;Also I recommend you not to change the original hosts file directly. First create a newhosts file with updated entries and then replace the hosts file&lt;BR /&gt;&lt;BR /&gt;Another thing - this flat file I suggest you create as following format&lt;BR /&gt;hostname changedipaddress&lt;BR /&gt;The reason behind this is in grep when we grep hostname it will match all occurances having that text&lt;BR /&gt;e.g. if you have server names as prod1 and prod1dr then when you grep prod - it will match both the entries, where as the method I have used is&lt;BR /&gt;grep "$hostnm "  so I have added a space after the hostname so that it will be an exact match for one server only and this is more easy if you have the flatfile with&lt;BR /&gt;hostname first then a space and then ipaddress.&lt;BR /&gt;&lt;BR /&gt;if [ `whoami` != "root" ]; then&lt;BR /&gt;echo "This script must be run with root permissions!"&lt;BR /&gt;exit&lt;BR /&gt;fi&lt;BR /&gt;d=$HOME/scripts/move&lt;BR /&gt;ips=$d/ips.list&lt;BR /&gt;if [ -f $d/hosts.new]; then&lt;BR /&gt;rm $d/$filename.new&lt;BR /&gt;fi&lt;BR /&gt;egrep -v "^#|^$|localhost" /etc/hosts | while read ipaddr hostnm remainingline&lt;BR /&gt; do&lt;BR /&gt;  if [[ $(grep -c -i "$hostnm " $ips) != "0" ]]&lt;BR /&gt;   then&lt;BR /&gt;      echo $ipaddr $hostnm $remainingline &amp;gt;&amp;gt; $d/hosts.new&lt;BR /&gt;   else&lt;BR /&gt;      echo "$(grep "$hostnm " $ips | awk '{print $2}') $hostnm $remainingline" &amp;gt;&amp;gt; $d/hosts.new&lt;BR /&gt;  fi&lt;BR /&gt; done&lt;BR /&gt;#cp /etc/hosts /etc/hosts.$(date +%Y%m%d%H%M)&lt;BR /&gt;#cp $d/hosts.new /etc/hosts&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;I have commented the last 2 entries - as I have not checked this script throughly - so request you to check first - if it is to your satisfaction remove the # and execute.&lt;BR /&gt;&lt;BR /&gt;Pls let know if you need any other help.&lt;BR /&gt;&lt;BR /&gt;Regards,&lt;BR /&gt;Ninad</description>
      <pubDate>Tue, 20 Jun 2006 07:32:15 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/search-and-replace-script/m-p/4986146#M100046</guid>
      <dc:creator>Ninad_1</dc:creator>
      <dc:date>2006-06-20T07:32:15Z</dc:date>
    </item>
    <item>
      <title>Re: Search and replace script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/search-and-replace-script/m-p/4986147#M100047</link>
      <description>&lt;!--!*#--&gt;Hi (again) Sally:&lt;BR /&gt;&lt;BR /&gt;Here's another Perl script to accomodate your changes.&lt;BR /&gt;&lt;BR /&gt;Modify the __DATA__ section of the script below to specify lines of pairs of IP addresses as 'before' and 'after' replacements.  The address pairs can be separated by blanks and/or tab characters.&lt;BR /&gt;&lt;BR /&gt;Then simply run the script passing your '/etc/hosts' file as its argument.  A backup copy of the unmodified file will be retained automatically as '/etc/hosts.old'.&lt;BR /&gt;&lt;BR /&gt;# cat .hostfix&lt;BR /&gt;#!/usr/bin/perl -i.old&lt;BR /&gt;use strict;&lt;BR /&gt;use warnings;&lt;BR /&gt;&lt;BR /&gt;my  @tokens;&lt;BR /&gt;my ($i, $x, $y);&lt;BR /&gt;{&lt;BR /&gt;    local $/ = undef;&lt;BR /&gt;    @tokens = split(/\s+/,&lt;DATA&gt;);&lt;BR /&gt;}&lt;BR /&gt;while (&amp;lt;&amp;gt;) {&lt;BR /&gt;    for ($i=0; $i &amp;lt; scalar @tokens; $i+=2) {&lt;BR /&gt;        ($x, $y) = @tokens [ $i, $i+1 ];&lt;BR /&gt;        s/$x/$y/;&lt;BR /&gt;    }&lt;BR /&gt;    print;&lt;BR /&gt;}&lt;BR /&gt;__DATA__&lt;BR /&gt;10.10.11.12     10.10.11.13&lt;BR /&gt;10.10.11.14     10.10.11.15&lt;BR /&gt;10.10.11.16     10.10.11.17&lt;BR /&gt;&lt;BR /&gt;...after modifying the __DATA__ lines, run as:&lt;BR /&gt;&lt;BR /&gt;# ./hostfix /etc/hosts&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...&lt;/DATA&gt;</description>
      <pubDate>Tue, 20 Jun 2006 08:11:45 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/search-and-replace-script/m-p/4986147#M100047</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2006-06-20T08:11:45Z</dc:date>
    </item>
    <item>
      <title>Re: Search and replace script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/search-and-replace-script/m-p/4986148#M100048</link>
      <description>&lt;!--!*#--&gt;jrf, that script is very dangerous, as your DATA section lists the IP's with dots, and the dots being special in regular expressions and you did not anchor the words&lt;BR /&gt;&lt;BR /&gt;my %change = qw{&lt;BR /&gt;    10.10.11.12     10.10.11.13&lt;BR /&gt;    10.10.11.14     10.10.11.15&lt;BR /&gt;    10.10.11.16     10.10.11.17&lt;BR /&gt;    };&lt;BR /&gt;&lt;BR /&gt;while (&amp;lt;&amp;gt;) {&lt;BR /&gt;    foreach my $org_ip (keys %change) {&lt;BR /&gt;        s/\b\Q$org_ip\E\b/$change{$org_ip}/;&lt;BR /&gt;        }&lt;BR /&gt;    print;&lt;BR /&gt;    }&lt;BR /&gt;&lt;BR /&gt;might be a bit more safe :)&lt;BR /&gt;&lt;BR /&gt;Enjoy, Have FUN! H.Merijn</description>
      <pubDate>Tue, 20 Jun 2006 08:32:31 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/search-and-replace-script/m-p/4986148#M100048</guid>
      <dc:creator>H.Merijn Brand (procura</dc:creator>
      <dc:date>2006-06-20T08:32:31Z</dc:date>
    </item>
    <item>
      <title>Re: Search and replace script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/search-and-replace-script/m-p/4986149#M100049</link>
      <description>Ok Ninad...I am almost there.  My only proble now is that my flat file will look like the following:&lt;BR /&gt;&lt;BR /&gt;10.l0.10.1  host&lt;BR /&gt;10.10.10.2  host-swc&lt;BR /&gt;&lt;BR /&gt;Then the host files are like the following:&lt;BR /&gt;&lt;BR /&gt;20.20.20.1 host.domain.com  host&lt;BR /&gt;20.20.20.2 anotherhost&lt;BR /&gt;20.20.20.3 host-swc&lt;BR /&gt;&lt;BR /&gt;So I go one step further in the script and echo $hostnm |awk -F. '{print $1}' so that I just have the name "host" .... but when the flat file is scanned for "host" it comes back with both host and host-swc and then the hosts.new gets screwed up.</description>
      <pubDate>Tue, 20 Jun 2006 08:46:04 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/search-and-replace-script/m-p/4986149#M100049</guid>
      <dc:creator>Coolmar</dc:creator>
      <dc:date>2006-06-20T08:46:04Z</dc:date>
    </item>
    <item>
      <title>Re: Search and replace script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/search-and-replace-script/m-p/4986150#M100050</link>
      <description>It seems that you already have pretty much mastered shell scripting, so just explaning you the logic to be changed and not the whole script :)&lt;BR /&gt;&lt;BR /&gt;Just wherever you are using grep to search the hostname occurance in flatfile, use&lt;BR /&gt;grep " $hostnm$" flatfilename&lt;BR /&gt;&lt;BR /&gt;So that it will check for a space before the hostname and check that there are no other characters after the hostname [ The $ at the end meaning ending with ]&lt;BR /&gt;&lt;BR /&gt;Hope this helps.&lt;BR /&gt;&lt;BR /&gt;Regards,&lt;BR /&gt;Ninad</description>
      <pubDate>Tue, 20 Jun 2006 08:59:59 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/search-and-replace-script/m-p/4986150#M100050</guid>
      <dc:creator>Ninad_1</dc:creator>
      <dc:date>2006-06-20T08:59:59Z</dc:date>
    </item>
    <item>
      <title>Re: Search and replace script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/search-and-replace-script/m-p/4986151#M100051</link>
      <description>Hi Merijn:&lt;BR /&gt;&lt;BR /&gt;Yes, you're right, of course.  I was relying entirely too much on decent data and failed to be rigouous.  At the least, I would have made my substitution:&lt;BR /&gt;&lt;BR /&gt;s/\b$x\b/$y/;&lt;BR /&gt;&lt;BR /&gt;Your variation helps me, though.  It addresses the dot character's ambiguity. Thanks, Merijn!&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Tue, 20 Jun 2006 09:00:21 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/search-and-replace-script/m-p/4986151#M100051</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2006-06-20T09:00:21Z</dc:date>
    </item>
    <item>
      <title>Re: Search and replace script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/search-and-replace-script/m-p/4986152#M100052</link>
      <description>Sorry,&lt;BR /&gt;One more thing - if you are using the modified script I gave - the latest script from my posts where I suggested flatfile format to be hostname and then ip address - change the $2 to $1 to get ip address from the flatfile as per your format.&lt;BR /&gt;&lt;BR /&gt;Regards,&lt;BR /&gt;Ninad</description>
      <pubDate>Tue, 20 Jun 2006 09:07:18 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/search-and-replace-script/m-p/4986152#M100052</guid>
      <dc:creator>Ninad_1</dc:creator>
      <dc:date>2006-06-20T09:07:18Z</dc:date>
    </item>
    <item>
      <title>Re: Search and replace script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/search-and-replace-script/m-p/4986153#M100053</link>
      <description>Ninad...&lt;BR /&gt;&lt;BR /&gt;I have the following (see below) and the hosts.new is good except for the entry with 10.10.10.1  host.domain.com host&lt;BR /&gt;That one doesn't change at all.&lt;BR /&gt;&lt;BR /&gt;if [ `whoami` != "root" ]; then&lt;BR /&gt;echo "This script must be run with root permissions!"&lt;BR /&gt;exit&lt;BR /&gt;fi&lt;BR /&gt;d=$HOME/scripts/move&lt;BR /&gt;ips=$d/ips.list&lt;BR /&gt;if [ -f $d/hosts.new ]; then&lt;BR /&gt;rm $d/$filename.new&lt;BR /&gt;fi&lt;BR /&gt;egrep -v "^#|^$|localhost" /etc/hosts | while read ipaddr hostnm remainingline&lt;BR /&gt;do&lt;BR /&gt;if [[ $(grep -c -i "$hostnm$" $ips) != "0" ]]&lt;BR /&gt;then&lt;BR /&gt;echo `grep $hostnm$ $ips`&amp;gt;&amp;gt; $d/hosts.new&lt;BR /&gt;else&lt;BR /&gt;echo "$ipaddr $hostnm $remainingline" &amp;gt;&amp;gt; $d/hosts.new&lt;BR /&gt;fi&lt;BR /&gt;done&lt;BR /&gt;#cp /etc/hosts /etc/hosts.$(date +%Y%m%d%H%M)&lt;BR /&gt;#cp $d/hosts.new /etc/hosts&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Tue, 20 Jun 2006 09:38:16 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/search-and-replace-script/m-p/4986153#M100053</guid>
      <dc:creator>Coolmar</dc:creator>
      <dc:date>2006-06-20T09:38:16Z</dc:date>
    </item>
    <item>
      <title>Re: Search and replace script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/search-and-replace-script/m-p/4986154#M100054</link>
      <description>What I am thinking is that I want to somehow grep the hosts file for $hostnm and then awk out the hosts with no "." in them.  So that would remove the host.domain.com strings and then it should work.  Right?  But I don't know how or if I can awk out the "." hosts.</description>
      <pubDate>Tue, 20 Jun 2006 09:48:41 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/search-and-replace-script/m-p/4986154#M100054</guid>
      <dc:creator>Coolmar</dc:creator>
      <dc:date>2006-06-20T09:48:41Z</dc:date>
    </item>
    <item>
      <title>Re: Search and replace script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/search-and-replace-script/m-p/4986155#M100055</link>
      <description>Actually, what I suggested above would be no good because then I would lose the IP address...DOH!&lt;BR /&gt;So my next thought is possibly to force grep somehow to take the $hostnm *exactly* has it appears...so $hostnm would scan the file for "host" only and not the other variations like "host-swc".  Is that possible?</description>
      <pubDate>Tue, 20 Jun 2006 09:56:06 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/search-and-replace-script/m-p/4986155#M100055</guid>
      <dc:creator>Coolmar</dc:creator>
      <dc:date>2006-06-20T09:56:06Z</dc:date>
    </item>
  </channel>
</rss>

