LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-24-2013, 03:05 PM   #1
123raajesh
LQ Newbie
 
Registered: Sep 2013
Posts: 12

Rep: Reputation: Disabled
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
 
Old 10-25-2013, 03:55 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
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.
 
2 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] add-apt-repostory command not found froylan Ubuntu 4 08-11-2012 08:44 AM
not able to add system call..getting error elf_i386 not found.. monaa Programming 0 04-29-2012 12:12 PM
[SOLVED] search, duplicate and add pattern with sed/awk lolworlds Programming 4 02-03-2012 11:17 AM
search pattern and add a line. sang_froid Programming 3 01-19-2012 10:47 AM
Installation Tree Not found when Add/remove programms barshani Linux - Software 0 10-11-2004 07:48 AM


All times are GMT -5. The time now is 01:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration