LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices



Reply
 
Search this Thread
Old 07-22-2009, 09:42 AM   #1
antcore
LQ Newbie
 
Registered: Jul 2009
Posts: 5

Rep: Reputation: 0
Using a long Bash command including single quotes and pipes in a Bash script


This long command works fine from the command line.

Code:
lynx -dump http://www.ip-adress.com/ip_tracer/?QRY=$1|grep address|egrep 'city|state|country'|awk '{print $3,$4,$5,$6,$7,$8}'|sed 's\ip address flag \\'|sed 's\My\\' | sed 's\address \\' |  perl -ne 'if (3..3) {print "$_"; last;}' |sed 's\city: \\'
However, if I want to use it in a script as such:

Code:
location=`lynx -dump http://www.ip-adress.com/ip_tracer/?QRY=$1|grep address|egrep 'city|state|country'|awk '{print $3,$4,$5,$6,$7,$8}'|sed 's\ip address flag \\'|sed 's\My\\' | sed 's\address \\' |  perl -ne 'if (3..3) {print "$_"; last;}' |sed 's\city: \\'`
I get

Code:
sed: -e expression #1, char 19: unterminated `s' command
sed: -e expression #1, char 5: unterminated `s' command
sed: -e expression #1, char 11: unterminated `s' command
sed: -e expression #1, char 9: unterminated `s' command
using

Code:
location=`'COMMAND'`
does not work because there are single quotes in the command. Trying

Code:
location=`'lynx -dump http://www.ip-adress.com/ip_tracer/?QRY=$1|grep address|egrep '\'' # etcetera
gives similar errors. Using double quotes and escaping all $'s also gives errors. There must be a smarter way to do this. Who'll educate me?
 
Old 07-22-2009, 10:18 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
It's recommended not to use backticks to embed commands. It's been superseded by the $() form. I don't get any errors when I use location=$(<your_long_command>).

Edit: By the way, you can usually use "sed -e 'expression1' -e 'expression2', to combine your filters into a single command.

Also, I can't be 100% certain because I don't know what your input value is supposed to be, but I'm guessing the error is coming from a '/' value being in the sed input somewhere, messing up the reading of the separators. Or something like that. You can change the sed separator to another character instead (e.g. sed -e 's|ip address flag ||') which also eliminates the error messages.


By the way, just what exactly are you trying to extract from the lynx output anyway? It's likely we can clean up the command and make it more efficient. I tried running it with the ip address of google.com and got nothing.

Last edited by David the H.; 07-22-2009 at 10:33 AM.
 
Old 07-22-2009, 10:29 AM   #3
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,049

Rep: Reputation: 378Reputation: 378Reputation: 378Reputation: 378
Besides that, using all of perl, awk, sed, egrep and grep is, to say the least, an overkill. Either awk or perl should be enough to do all the parsing, or even sed. I'd also drop lynx in favor of wget -O - if possible, it seems to be much standard these days, but that's just my opinion.

I haven't looked deeply into the purpose of that one-liner, but it seems to me that you are creating complication for nothing.
 
Old 07-22-2009, 10:31 AM   #4
antcore
LQ Newbie
 
Registered: Jul 2009
Posts: 5

Original Poster
Rep: Reputation: 0
This works. Thank you.
 
Old 07-22-2009, 10:34 AM   #5
antcore
LQ Newbie
 
Registered: Jul 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks for the solution regarding the syntax, David the H.

With regard to the command itself. It is not a beauty, yet. With regard to what it does, it gives back the name of the city your provider is in, based on your IP.
 
Old 07-22-2009, 10:41 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
Well, I'd help you to find a better way, but all I'm getting now is a "You reached your limit of 10 lookup queries per day" message, so I can't test it anymore. If you could provide a sample of the output we could work from instead, I'm sure we could give you a cleaner way to get that info.
 
Old 07-22-2009, 10:57 AM   #7
antcore
LQ Newbie
 
Registered: Jul 2009
Posts: 5

Original Poster
Rep: Reputation: 0
My goal was to get a string representing the current city in which I am. (I push this string to a list which serves as the source for a web page on which some colleagues of mine can read my latest location update.) Looking for a simple way to extract my current location, I came across a working one liner at http://www.commandlinefu.com/ which produced relevant output for me:

Code:
address country: Netherlands
address state: Zuid-Holland   
address city: Leiden
Since I did not need the first two lines and only the third word of the last line, I added:

Code:
 | sed 's\address \\' |  perl -ne 'if (3..3) {print "$_"; last;}' |sed 's\city: \\'
which gives me:

Code:
Leiden
 
Old 07-22-2009, 11:16 AM   #8
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,049

Rep: Reputation: 378Reputation: 378Reputation: 378Reputation: 378
I am not testing this, but, the first one you posted is grep'ing for "address", then whatever else.

Why not just grep for "address city" instead if that's all you want? Then just sed it to remove "address city: ".

Code:
<whatever to fetch the page> | grep "address city:" | sed -e 's/address city: //'
Much simpler. Just test it step by step, first the grep, then based on the output, look what do you exactly need to cut with sed, and so on.
 
Old 07-22-2009, 11:22 AM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
I'd need to see the exact input in order to make sure that we can always match the city name. For example, is there only one line with the phrase "address city" in it, or could there be multiple hits?

If, for example, there's only one line with "address city: cityname" in it, and nothing following it but a newline, then it could be as simple as
Code:
<input>|sed -nr s/'^.*address city: (.+)$/\1/p'
I'm thinking it might not be as easy as that though, or else the original line probably wouldn't be as complex as it is.

Last edited by David the H.; 07-22-2009 at 11:27 AM.
 
Old 07-22-2009, 12:10 PM   #10
antcore
LQ Newbie
 
Registered: Jul 2009
Posts: 5

Original Poster
Rep: Reputation: 0
David the H. is right:

Code:
lynx -dump http://www.ip-adress.com/ip_tracer/?QRY=$1|sed -nr s/'^.*My IP address city: (.+)$/\1/p'
and all is fine.

With regard to i92guboj's remark about lynx: w3m also does the trick, wget gives a 403.

Last edited by antcore; 07-22-2009 at 12:28 PM. Reason: reaction to i92guboj
 
  


Reply

Tags
bash


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Scope of unnamed pipes in BASH script propofol Programming 13 06-17-2009 01:54 AM
LXer: Using Bash To Feed Command Output To A While Loop Without Using Pipes! LXer Syndicated Linux News 0 08-06-2008 01:10 PM
Including binary file into bash script Barca Programming 2 07-01-2005 01:45 PM
In BASH shell, what is the difference in usage between single and double quotes? davidas Linux - Newbie 2 04-05-2004 04:00 AM
same find command not working in bash script, quotes? QuakerOatz Linux - Software 1 07-14-2003 01:04 PM


All times are GMT -5. The time now is 12:56 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration