Go Back   HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials > Linux Forums > Programming/Scripts

Do you like HowtoForge? Please consider supporting us by becoming a subscriber.
Reply
 
Thread Tools Display Modes
  #1  
Old 16th August 2007, 20:40
Mosquito Mosquito is offline
Member
 
Join Date: Nov 2006
Posts: 85
Thanks: 5
Thanked 6 Times in 5 Posts
Default Put text from multiple lines onto one line?

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.
Reply With Quote
Sponsored Links
  #2  
Old 16th August 2007, 22:56
sjau sjau is offline
Local Meanie
 
Join Date: Apr 2006
Location: Switzerland
Posts: 1,146
Thanks: 4
Thanked 55 Times in 51 Posts
Default

have you php installed?
Reply With Quote
  #3  
Old 16th August 2007, 23:23
Mosquito Mosquito is offline
Member
 
Join Date: Nov 2006
Posts: 85
Thanks: 5
Thanked 6 Times in 5 Posts
Default

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?
Reply With Quote
  #4  
Old 17th August 2007, 01:18
sjau sjau is offline
Local Meanie
 
Join Date: Apr 2006
Location: Switzerland
Posts: 1,146
Thanks: 4
Thanked 55 Times in 51 Posts
Default

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.
Reply With Quote
The Following User Says Thank You to sjau For This Useful Post:
Mosquito (5th October 2007)
  #5  
Old 4th October 2007, 22:02
catdude catdude is offline
Senior Member
 
Join Date: Jun 2007
Posts: 178
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Quote:
Originally Posted by Mosquito
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.
How about something like this:

cat output.txt | awk ' $1 ~ /^Lat/ { _lat = $2; }
$1 ~ /^Long/ { _long = $2; }
$1 ~ /^Guess/ { print _lat, _long; }'
Reply With Quote
The Following User Says Thank You to catdude For This Useful Post:
Mosquito (5th October 2007)
  #6  
Old 5th October 2007, 16:46
Mosquito Mosquito is offline
Member
 
Join Date: Nov 2006
Posts: 85
Thanks: 5
Thanked 6 Times in 5 Posts
 
Default

Quote:
Originally Posted by catdude
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.
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Incoming Mail Deleted? Jammer4774 General 19 20th July 2007 23:06
reoback problems on debian 3.1 Ovidiu Server Operation 12 15th May 2007 23:25
Anyone figured out to integrate FuzzyOCR into ISPConfig on Ubuntu? friday Installation/Configuration 16 10th May 2007 17:25
Apache2 don't start /var/www/sharedip don't exist Dr.Oetker General 14 6th May 2007 14:15
help on /etc/rc0.d/K20dnsctl -> ../init.d/dnsctl zimele General 13 23rd August 2006 17:06


All times are GMT +2. The time now is 11:35.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.