LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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 02-08-2013, 01:09 AM   #1
alyda
LQ Newbie
 
Registered: Feb 2013
Posts: 3

Rep: Reputation: Disabled
question about piping grep into sed into xargs for geektool weather script


Hi, I'm working with unix on Mac OSX. I'm learning how to work with unix by creating geektool scripts. I want the following output:

Clear, 56.2 F

what I'm getting is this:

Clear 56.2 F

very slight change (addition of a comma, removal of extra space produced by xargs), I know, but I'm trying to learn how to control output from the following:

Code:
curl -s http://api.wunderground.com/api/key/...6.1.99999.json | egrep --color '"weather"|"temp_f"' | sed 's/[[:blank:]"[:alpha:]_]*:"*\([[:alpha:][:blank:][:digit:].]*\)"*,/\1/' | xargs -n2 | xargs -I% echo % "F"
note: you will need to register and get a free api key from wunderground.com

thanks in advance
 
Old 02-08-2013, 01:12 AM   #2
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 582

Rep: Reputation: 129Reputation: 129
You can use printf instead of echo it's more versatile.
 
Old 02-08-2013, 01:28 AM   #3
alyda
LQ Newbie
 
Registered: Feb 2013
Posts: 3

Original Poster
Rep: Reputation: Disabled
I can see from the man page for printf that its probably what I'm looking for. But could you please provide an example? below is example output from the API

Code:
{
	"response": {
		"version": "0.1"
		,"termsofService": "http://www.wunderground.com/weather/api/d/terms.html"
		,"features": {
		"conditions": 1
		}
	}
		,	"current_observation": {
		"image": {
		"url":"http://icons-ak.wxug.com/graphics/wu2/logo_130x80.png",
		"title":"Weather Underground",
		"link":"http://www.wunderground.com"
		},
		"display_location": {
		"full":"Mammoth Lakes, CA",
		"city":"Mammoth Lakes",
		"state":"CA",
		"state_name":"California",
		"country":"US",
		"country_iso3166":"US",
		"zip":"93546",
		"latitude":"37.65124893",
		"longitude":"-118.98217010",
		"elevation":"2458.00000000"
		},
		"observation_location": {
		"full":"Juniper Springs Lodge (760)924-1102, Mammoth Lakes, California",
		"city":"Juniper Springs Lodge (760)924-1102, Mammoth Lakes",
		"state":"California",
		"country":"US",
		"country_iso3166":"US",
		"latitude":"37.635544",
		"longitude":"-118.989830",
		"elevation":"7953 ft"
		},
		"estimated": {
		},
		"station_id":"KCAMAMMO4",
		"observation_time":"Last Updated on February 7, 11:23 PM PST",
		"observation_time_rfc822":"Thu, 07 Feb 2013 23:23:59 -0800",
		"observation_epoch":"1360308239",
		"local_time_rfc822":"Thu, 07 Feb 2013 23:24:18 -0800",
		"local_epoch":"1360308258",
		"local_tz_short":"PST",
		"local_tz_long":"America/Los_Angeles",
		"local_tz_offset":"-0800",
		"weather":"Clear",
		"temperature_string":"20.4 F (-6.4 C)",
		"temp_f":20.4,
		"temp_c":-6.4,
		"relative_humidity":"72%",
		"wind_string":"Calm",
		"wind_dir":"NE",
		"wind_degrees":42,
		"wind_mph":0.0,
		"wind_gust_mph":0,
		"wind_kph":0.0,
		"wind_gust_kph":0,
		"pressure_mb":"1008",
		"pressure_in":"29.76",
		"pressure_trend":"-",
		"dewpoint_string":"13 F (-11 C)",
		"dewpoint_f":13,
		"dewpoint_c":-11,
		"heat_index_string":"NA",
		"heat_index_f":"NA",
		"heat_index_c":"NA",
		"windchill_string":"20 F (-6 C)",
		"windchill_f":"20",
		"windchill_c":"-6",
		"feelslike_string":"20 F (-6 C)",
		"feelslike_f":"20",
		"feelslike_c":"-6",
		"visibility_mi":"10.0",
		"visibility_km":"16.1",
		"solarradiation":"",
		"UV":"0",
		"precip_1hr_string":"-999.00 in ( 0 mm)",
		"precip_1hr_in":"-999.00",
		"precip_1hr_metric":" 0",
		"precip_today_string":"0.00 in (0 mm)",
		"precip_today_in":"0.00",
		"precip_today_metric":"0",
		"icon":"clear",
		"icon_url":"http://icons-ak.wxug.com/i/c/k/nt_clear.gif",
		"forecast_url":"http://www.wunderground.com/US/CA/Mammoth_Lakes.html",
		"history_url":"http://www.wunderground.com/weatherstation/WXDailyHistory.asp?ID=KCAMAMMO4",
		"ob_url":"http://www.wunderground.com/cgi-bin/findweather/getForecast?query=37.635544,-118.989830"
	}
}
I'm not exactly sure how to extract 2 or more pieces of data from wunderground.com, formatting them differently, then joining the results together again. I may be able to discover this on my own, but my searches on google have not solved my problem thus far.

Thanks again.
 
Old 02-08-2013, 01:54 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Can't you use awk to do all the work instead of multiple commands and pipes?

I'm not able to test this on OS-X, but this one-liner seems to work on linux when using the output ptovided in post #3:
Code:
awk -F[:,] '/"weather"/ { gsub(/"/,"") ; WEATHER=$2 } /"temp_f"/{ TEMP=$2 }END{ print WEATHER",",TEMP,"F" }'
In a bit more readable form:
Code:
awk -F[:,] '                                # set multiple separators (: and ,)
/"weather"/ { gsub(/"/,"") ; WEATHER=$2 }   # search for "weather", remove both " and store field 2 in variable
/"temp_f"/{ TEMP=$2 }                       # search for "tem_f" and store field 2 in variable
END{ print WEATHER",",TEMP,"F" }'           # print any way you want (you could use printf if you want)
A sample run on the output provided:
Code:
$ awk -F[:,] '/"weather"/ { gsub(/"/,"") ; WEATHER=$2 } /"temp_f"/{ TEMP=$2 }END{ print WEATHER",",TEMP,"F" }' api.output
Clear, 20.4 F
 
Old 02-11-2013, 10:07 PM   #5
alyda
LQ Newbie
 
Registered: Feb 2013
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thanks for your code, druuna. the following is giving me an error on OSX:

Code:
curl -s http://api.wunderground.com/api/9906888802114107/conditions/q/zmw:93546.1.99999.json" | awk -F[:,] '/"weather"/ { gsub(/"/,"") ; WEATHER=$2 } /"temp_f"/{ TEMP=$2 }END{ print WEATHER",",TEMP,"F" }'
error:
Code:
-bash: syntax error near unexpected token `)'
So I'm trying to learn exactly how AWK works.

Code:
curl -s "http://api.wunderground.com/api/9906888802114107/conditions/q/zmw:93546.1.99999.json" | awk '/"weather"/'
returns the first line i'm looking for,

Code:
curl -s "http://api.wunderground.com/api/9906888802114107/conditions/q/zmw:93546.1.99999.json" | awk '/"weather"/ { gsub(/"/,""); print $1 }'
removes the " and blank space from the output

Code:
curl -s "http://api.wunderground.com/api/9906888802114107/conditions/q/zmw:93546.1.99999.json" | awk -F[:,] '/"weather"/ { gsub(/"/,""); WEATHER=$2 } /"temp_f"/ { TEMP=$2 } END { print WEATHER",",TEMP,"F" } '
the above line works for me, I don't see a difference between your solution and mine (maybe the problem arises because I tried to copy / paste your code into terminal). The only minor issue is that there is still a space after the 2nd result and before the degree symbol:

Code:
Clear, 21.8 F
 
Old 02-12-2013, 03:11 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Quote:
Originally Posted by alyda View Post
The only minor issue is that there is still a space after the 2nd result and before the degree symbol:

Code:
Clear, 21.8 F
You can fix that by removing the last comma in the END part:
Code:
# without the space
{ print WEATHER",",TEMP"F" }

# with a space
{ print WEATHER",",TEMP,"F" }
 
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Script to remove dynamic array from file - sed or grep marky9074 Programming 20 11-09-2012 08:17 AM
Help to remove script data from file using grep & sed djlane Programming 1 07-13-2010 08:10 AM
grep+awk+sed+paste+sort in one script? mchriste Linux - Software 13 03-05-2009 01:57 PM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM
sed & grep script? dolvmin Linux - Software 20 09-22-2003 06:30 AM


All times are GMT -5. The time now is 07:37 AM.

Main Menu
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