LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   question about piping grep into sed into xargs for geektool weather script (http://www.linuxquestions.org/questions/linux-general-1/question-about-piping-grep-into-sed-into-xargs-for-geektool-weather-script-4175449119/)

alyda 02-08-2013 01:09 AM

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

whizje 02-08-2013 01:12 AM

You can use printf instead of echo it's more versatile.

alyda 02-08-2013 01:28 AM

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.

druuna 02-08-2013 01:54 AM

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


alyda 02-11-2013 10:07 PM

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

druuna 02-12-2013 03:11 AM

Quote:

Originally Posted by alyda (Post 4889595)
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" }



All times are GMT -5. The time now is 05:45 PM.