I am new to shell scripting and would appreciate some help. First some background - I want to display picture showing hosts around the world that have accessed a specific file on a server. All that is logged about the hosts is IP address - which is fine. From these IP addresses I can get a general idea of their location - again fine - via http://api.hostip.info/ (a geolocator). Basically, this is to provide a quick visual. It doesn't need to be extremely detailed. Now, the first part I can handle - converting IP address to Latitude/Longitude. This is done via the following script: Code: #!/bin/sh touch output.txt for line in $(< hosts);do lynx -dump "http://api.hostip.info/rough.php?ip=$line&position=true" >> output.txt done exit 0 This outputs a file with the following format Code: Country: FRANCE Country Code: FR City: Paris Latitude: 48.8 Longitude: 2.33333 Guessed: false Country: UNITED STATES Country Code: US City: Bridgeport, CT Latitude: 41.1863 Longitude: -73.1962 Guessed: true This is where I am stuck. The only data I need from this format is the latitudes and the longitudes. Each lat/long pair needs to be on its own line. Example: Code: 48.8 2.33333 41.1863 -73.1962 My question is: How do I pull just the latitude and longitude from output.txt and put them in the above format? Thank you.
I do have PHP installed. Version 5.2.x (can't remember right off hand). I welcome a PHP based solution, however, will that significantly slow down processing if I am calling a PHP script from within a shell script?
it will slow down but not much... you'll have to install php5-cli so you can run php from the command line Then you would have your shell script: Code: #!/bin/sh touch output.txt for line in $(< hosts);do lynx -dump "http://api.hostip.info/rough.php?ip=$line&position=true" >> output.txt php parse.php done exit 0 and the php script would be something like this: Code: <?php // Read the file content into a variable $filename = "output.txt"; // if this is not working, use an absolut path: "/path/to/file.txt" $handle = fopen ($filename, "r"); $contents = fread ($handle, filesize ($filename)); fclose ($handle); // Explode the file content at the line break $contents = explode("\n", $contents); // Loop through the array $i = 1; foreach($contents as $val) { $pos = strpos($val, "Latitude"); if ($pos === false) { } else { $val = explode(": ", $val); $val = $val[1]; $result = "result" . $i; $$result = $val; $i++; } $pos = strpos($val, "Longitude"); if ($pos === false) { } else { $val = explode(": ", $val); $val = $val[1]; $result = "result" . $i; $$result = $val; $i++; } } // Open file for writing $content_new = $result1 . " " . $result2 . "\n" . $result3 . " " . $result4; if (is_writable($filename)) { if (!$handle = fopen($filename, "w+")) { print "cannot open $filename"; exit; } if (!fwrite($handle, $content_new)) { print "Cannot write to $filename"; exit; } print "Done, new content written to $filename"; fclose($handle); } else { print "$filename is not writeable"; } ?> I have not tested the script and hence I assume it still has some bugs... but you should be able to work that out.
How about something like this: cat output.txt | awk ' $1 ~ /^Lat/ { _lat = $2; } $1 ~ /^Long/ { _long = $2; } $1 ~ /^Guess/ { print _lat, _long; }'
I'll give this a try this evening. Thank you. I like the idea of not having to use a php script to do this.