LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   search pattern if found do not add if not found add (https://www.linuxquestions.org/questions/linux-newbie-8/search-pattern-if-found-do-not-add-if-not-found-add-4175482080/)

123raajesh 10-24-2013 02:05 PM

search pattern if found do not add if not found add
 
Hi Colucix,

I am trying to get a result out of this but fails please help. Have two files /tmp/1 & /tmp/hosts.

/tmp/1
IP=123.456.789.01
WAS_HOSTNAME=abcdefgh.was.tb.dsdc

/tmp/hosts
123.456.789.01

I want this result in /tmp/hosts if hostname is already there dont want duplicate entry.

This is wrong
123.456.789.01 abcdefgh.was.tb.dsdc abcdefgh.was.tb.dsdc

This is correct
123.456.789.01 abcdefgh.was.tb.dsdc

Trying with this:

if grep abcdefgh.was.tb.dsdc /tmp/hosts; then
. /tmp/1 && sed -i "s/^$IP\(.*\)$/$IP\1 $WAS_HOSTNAME/" /tmp/hosts;
fi

Thanks in advance

druuna 10-25-2013 02:55 AM

The if grep abcdefgh.was.tb.dsdc /tmp/hosts part checks if the string is present, you need to know if the string is not present.

Try this:
Code:

if ! grep abcdefgh.was.tb.dsdc /tmp/hosts
then
  . /tmp/1 && sed -i "s/^$IP$/$IP $WAS_HOSTNAME/" /tmp/hosts
fi

I also changed your sed statement, yours seems to elaborate.


All times are GMT -5. The time now is 02:46 AM.