LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-18-2010, 04:30 PM   #1
NetRock
Member
 
Registered: Mar 2010
Posts: 134

Rep: Reputation: 16
Question Update a line using AWK with condition


Hi

Need your help to solve this issue, appreciate your help.

i have a host file called myhosts.txt as follow:
Quote:
10.20.3.9 host1
10.20.3.10 host2
10.20.3.11 host3
10.20.3.12 host4
10.20.3.13 host5
10.20.3.14 host6
10.20.3.18 host7
................
................
10.20.3.120 host120
In my script, i am using NSLOOKUP to get the ip address of a host that just rebooted and connected to my network (this part has been done).But now i want to check the myhosts.txt file if the host has the same or different IP address, if is different then myhosts.txt file must be updated with the new IP.

i am using the following AWk command:

Quote:
awk '/host4/{$1=val}{print}' val=10.0.3.4 /tmp/myhosts.txt
it prints on screen fine and i see the host4's ip address gets replaced but the actual file does not get updated with the new IP. I might need to use gsub?? any ideas......

Thanks
 
Old 06-18-2010, 04:34 PM   #2
alunduil
Member
 
Registered: Feb 2005
Location: San Antonio, TX
Distribution: Gentoo
Posts: 684

Rep: Reputation: 62
Why not use sed for this purpose? Also wouldn't setting up bind with dynamic DNS updates be easier? Just asking out of curiosity.

Regards,

Alunduil
 
Old 06-18-2010, 04:39 PM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
awk does not change the input file. You have to redirect the output to a temporary file, then rename it as the original. On the contrary sed, as suggested by alunduil, can edit the file in place (using -i option) saving the additional mv command.
 
Old 06-19-2010, 09:33 PM   #4
NetRock
Member
 
Registered: Mar 2010
Posts: 134

Original Poster
Rep: Reputation: 16
Thanks for the direction i would use something like this:

Quote:
sed -i 's/host4/10.0.3.4/g' /tmp/myhosts.txt
due the way the project is done not able to use dynamic DNS updates...

have a great day!
 
Old 06-20-2010, 01:16 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,619

Rep: Reputation: 2940Reputation: 2940Reputation: 2940Reputation: 2940Reputation: 2940Reputation: 2940Reputation: 2940Reputation: 2940Reputation: 2940Reputation: 2940Reputation: 2940
Hey Netrock

Not quite right assuming you do not want to simply replace "host4" with the ip address.
Based on your example I would say the change looks like the following (please correct me if i am wrong)
Code:
10.20.3.9 host1
10.20.3.10 host2
10.20.3.11 host3
10.20.3.12 host4
10.20.3.13 host5
10.20.3.14 host6
10.20.3.18 host7
................
................
10.20.3.120 host120
becomes:
Code:
10.20.3.9 host1
10.20.3.10 host2
10.20.3.11 host3
10.0.3.4 host4
10.20.3.13 host5
10.20.3.14 host6
10.20.3.18 host7
................
................
10.20.3.120 host120
Assuming the above is correct you will be looking to do something like:
Code:
sed -i -r 's/^.*(host4$)/10.0.3.4 \1/g' /tmp/myhosts.txt
 
Old 06-21-2010, 08:06 AM   #6
NetRock
Member
 
Registered: Mar 2010
Posts: 134

Original Poster
Rep: Reputation: 16
Hi grail,

just want to say THANKS for your help & have a HAPPY day!!!
 
Old 06-21-2010, 08:59 AM   #7
NetRock
Member
 
Registered: Mar 2010
Posts: 134

Original Poster
Rep: Reputation: 16
Hi...

Just got an issue. when i run the command from Linux machine is ok. but when i go to windows from command prompt, run the code where i have the sed command i get the following error:

Quote:
sed: -e expression #1, char1:unknown command


just to let you know i am using sed in my batch file where the hots4 & the Ip address are variables with %host% & %ipaddress%
i am running the batch file under the dir. where i have the sed command.

thanks for your help.
 
Old 06-21-2010, 09:36 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,619

Rep: Reputation: 2940Reputation: 2940Reputation: 2940Reputation: 2940Reputation: 2940Reputation: 2940Reputation: 2940Reputation: 2940Reputation: 2940Reputation: 2940Reputation: 2940
Sorry not sure how windows sed performs (ie. if exactly the same) also not sure how the variables will be interpreted, but,
if you were doing it in bash you know you have to leave the confines of the statement to have a variable work?

eg.
Code:
sed -i -r 's/^.*('$host4'$)/10.0.3.4 \1/g' /tmp/myhosts.txt
Here $host4 is now a variable from the outer shell or your script.

Hope that helps.
 
Old 06-21-2010, 09:40 AM   #9
NetRock
Member
 
Registered: Mar 2010
Posts: 134

Original Poster
Rep: Reputation: 16
Good day grail

i did the following:

Quote:
sed -i -r "s/^.*('$host4'$)/10.0.3.4 \1/g" /tmp/myhosts.txt
(used " and that fixed the error. Thanks a lot for your quick reply.
 
Old 06-21-2010, 11:07 AM   #10
NetRock
Member
 
Registered: Mar 2010
Posts: 134

Original Poster
Rep: Reputation: 16
Hi grail,

continue going further down now looking for get these two things done:
1) between the IP address & hots i have a space(tab) but after running the code i get no space between the ip address & the host

2) how can i Extract the last character of my variable:
for example if i have "host4" i want to update "host" too or if i have "hostname1" i need to update "hostname" with the same Ip address. i tried piping it to head -c -1 but that did not help.

your help appreciated.
 
Old 06-21-2010, 04:48 PM   #11
NetRock
Member
 
Registered: Mar 2010
Posts: 134

Original Poster
Rep: Reputation: 16
Hi ....

following gives me No.2

Quote:
echo %host%|sed "$s/.$//"
but not able to assign the output to any variable.
if i use
Quote:
set k=`echo %host%|sed "$s/.$//"`
sed does not like the ` again getting the error:

Quote:
sed: -e expression #1, char7:unknown command
any idea how i can assign it to a variable so i can use it in my batch file?

Thanks.
 
Old 06-21-2010, 08:16 PM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,619

Rep: Reputation: 2940Reputation: 2940Reputation: 2940Reputation: 2940Reputation: 2940Reputation: 2940Reputation: 2940Reputation: 2940Reputation: 2940Reputation: 2940Reputation: 2940
The error says "unknown command" and i would have to agree as '$s' is not a command (that I know of)

As for retaining the whitespace, you will need to add this to your sed:
Code:
sed -i -r "s/^.*(\w$host4$)/10.0.3.4 \1/g" /tmp/myhosts.txt
 
  


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
need to create awk sript for generating two files from a file checking a condition vandana.parwani Programming 3 03-03-2010 11:15 AM
awk command line: blank line record sep, new line field sep robertmarkbram Programming 4 02-21-2010 05:25 AM
IF condition in shell command line stepic Programming 3 04-23-2009 02:20 AM
how to replace line of file with another line using awk in shell script amit_pansuria Programming 3 03-29-2009 09:43 AM
grab the line below a blank line and the line above the next blank line awk or perl? Pantomime Linux - General 7 06-26-2008 08:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:40 AM.

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