LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 03-02-2009, 04:35 AM   #1
NickJH
LQ Newbie
 
Registered: Mar 2009
Distribution: ClarkConnect CE5.0
Posts: 19

Rep: Reputation: 0
Help with BASH script and AWK and SED


Hi,

I am pretty much new to hands on Linux, but I've been following ClarkConnect (based on RHEL) for a while. I built a box a week ago and now I find I need to write my first script. As part of the script, I am trying to resolve a URL into an IP address and put the IP address into a variable. I can find the IP address with this:
Code:
host www.bbc.co.uk | awk '$3 == "address" { print $4 }'
but I cannot find a way of assigning the result to a variable in a BASH script e.g.
Code:
NEWADD= host www.bbc.co.uk | awk '$3 == "address" { print $4 }'
does not work. I have also tried putting single and double quote marks round the command and escaping quotes within the command but I cannot get anything to work.

As a second (minor) issue, the following works:
Code:
sed 's/right=[0123456789]*\.[0123456789]*/right=123.123/' testfile.txt > testfile.txt.new
but replacing [0123456789] with \d does not. (The code is shortened to fit the screen. I am really trying to replace one IP address with another where it occues after a string "right="). Is there any way of using \d to make the code look cleaner?

For my testing I am using Ubuntu in a Windows VirtualBox VM as it has a nice GUI whereas CC is test only.

Thanks,

Nick
 
Old 03-02-2009, 04:40 AM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
hi, and welcome to LQ!

Almost there ;}

Code:
NEWADD=$( host www.bbc.co.uk | awk '$3 == "address" { print $4 }' )

And no,
[0123456789] replaced with \d will only work in perl,
but you can use [0-9]

Cheers,
Tink

Last edited by Tinkster; 03-02-2009 at 04:44 AM. Reason: added [0123456789] comment
 
Old 03-02-2009, 05:49 AM   #3
NickJH
LQ Newbie
 
Registered: Mar 2009
Distribution: ClarkConnect CE5.0
Posts: 19

Original Poster
Rep: Reputation: 0
Thanks. Both work now, but I guess you can see where this is going with the sed command.

What I am trying now is to replace an IP address in a file with another one based on a url:
Code:
NEWADD=$( host www.bbc.co.uk | awk '$3 == "address" { print $4 }' )
sed 's/right=[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*/right=$NEWADD/' testfile.txt > testfile.txt.new
This didn't work but if I replace the ' with " in the sed command so:
Code:
NEWADD=$( host www.bbc.co.uk | awk '$3 == "address" { print $4 }' )
sed "s/right=[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*/right=$NEWADD/" testfile.txt > testfile.txt.new
it works OK.

Can I make the input file and output file the same so I am just changing the text in the input file?

/[edit]correct code[/edit]

Last edited by NickJH; 03-02-2009 at 05:52 AM.
 
Old 03-02-2009, 06:19 AM   #4
NickJH
LQ Newbie
 
Registered: Mar 2009
Distribution: ClarkConnect CE5.0
Posts: 19

Original Poster
Rep: Reputation: 0
Never mind.

To answer my own question, use the -i switch to change the command to:
Code:
sed -i "s/right=[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*/right=$NEWADD/" testfile.txt
 
Old 03-06-2009, 01:59 PM   #5
NickJH
LQ Newbie
 
Registered: Mar 2009
Distribution: ClarkConnect CE5.0
Posts: 19

Original Poster
Rep: Reputation: 0
I now have my script written and I'd like it to loop forever, using the sleep command to wait 120 seconds between loops. I cannot find any obvious looping command apart from executing something like a for loop or an until loop which never terminates. Is there anything like a goto or jump statement? If not, what is the best way to achieve what I want?
 
Old 03-06-2009, 05:25 PM   #6
T74marcell
Member
 
Registered: Mar 2009
Posts: 102

Rep: Reputation: 18
Backticks do also work:

Quote:
NEWADD=`host www.bbc.co.uk | awk '$3 == "address" { print $4 }'`
Arch Linux

Last edited by T74marcell; 03-14-2009 at 02:03 AM.
 
Old 03-06-2009, 07:43 PM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by T74marcell View Post
Backticks do also work:
They do ... but they're usually not as good a choice
because they can't be nested ....


Cheers,
Tink
 
Old 03-06-2009, 08:21 PM   #8
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,842

Rep: Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823
Quote:
Originally Posted by NickJH View Post
Is there anything like a goto or jump statement? If not, what is the best way to achieve what I want?
"Best" is always personal choice - I'd use a "while true; do ...done" stick a test inside the loop with a break if you have a known condition to test against.
Go get the abs-guide at tldp.org
 
Old 03-06-2009, 08:26 PM   #9
GazL
Senior Member
 
Registered: May 2008
Posts: 4,087
Blog Entries: 1

Rep: Reputation: 1535Reputation: 1535Reputation: 1535Reputation: 1535Reputation: 1535Reputation: 1535Reputation: 1535Reputation: 1535Reputation: 1535Reputation: 1535Reputation: 1535
Quote:
Originally Posted by NickJH View Post
I now have my script written and I'd like it to loop forever, using the sleep command to wait 120 seconds between loops. I cannot find any obvious looping command apart from executing something like a for loop or an until loop which never terminates. Is there anything like a goto or jump statement? If not, what is the best way to achieve what I want?
You sort of answered your own question there... A while or until loop is the obvious solution.

If you want it to loop forever, then you can do a
Code:
while sleep 120
do
  your stuff here
done
Not quite sure why you'd want a goto, those things belong to the 1980s.
 
Old 03-07-2009, 03:19 AM   #10
NickJH
LQ Newbie
 
Registered: Mar 2009
Distribution: ClarkConnect CE5.0
Posts: 19

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by GazL View Post
Not quite sure why you'd want a goto, those things belong to the 1980s.
Totally agree, but I've never wanted to write a program with an infinite loop before and it seems conceptually odd to use a conditional loop with a condition which can never be met or is always met. A goto seemed a way round, but it does not seem to be the bash way of doing it.

I like your while code, and I may get away with the sleep test first. Alternatively I'll try something like while [ 1=1 ] and put the sleep at the end of my code.
 
Old 03-07-2009, 06:49 AM   #11
GazL
Senior Member
 
Registered: May 2008
Posts: 4,087
Blog Entries: 1

Rep: Reputation: 1535Reputation: 1535Reputation: 1535Reputation: 1535Reputation: 1535Reputation: 1535Reputation: 1535Reputation: 1535Reputation: 1535Reputation: 1535Reputation: 1535
Quote:
Originally Posted by NickJH View Post
Totally agree, but I've never wanted to write a program with an infinite loop before and it seems conceptually odd to use a conditional loop with a condition which can never be met or is always met. A goto seemed a way round, but it does not seem to be the bash way of doing it.

I like your while code, and I may get away with the sleep test first. Alternatively I'll try something like while [ 1=1 ] and put the sleep at the end of my code.
I understand what your getting at, a non-conditional conditional loop does seem a bit like a hack.

You don't need to use [ 1 = 1 ] though, there's a neater way if you want to keep the sleep within the loop itself. As syg00 hinted above, you can use

Code:
while true
do
 stuff
 sleep 120
done
 
Old 03-07-2009, 05:08 PM   #12
NickJH
LQ Newbie
 
Registered: Mar 2009
Distribution: ClarkConnect CE5.0
Posts: 19

Original Poster
Rep: Reputation: 0
I like it. Thanks.

Nick
 
  


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
bash - awk, sed, grep, ... advice schneidz Programming 13 08-25-2008 10:30 AM
N00b's bash, sed, or awk question petcherd Slackware 5 08-01-2007 07:42 AM
Replacing a place holder on a template with bash, sed, and or awk rignes Programming 7 02-16-2006 05:20 PM
Simple bash/awk/sed scripting question R00ts Programming 4 04-16-2005 03:55 AM
How to loop or sort in bash, awk or sed? j4r0d Programming 1 09-09-2004 04:22 AM


All times are GMT -5. The time now is 02:37 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