LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-10-2010, 09:48 PM   #1
stodds
LQ Newbie
 
Registered: May 2010
Posts: 6

Rep: Reputation: 0
Getting Numbers From A Line Of Text


Hey Guys,

This is my first post and hopefully I can get a bit of help. I'm working with a rather large file of data taking from a tracking program on my phone, and trying to pull only the longitude and latitude from it.

Any given line in the data looks more or less like this:

{"lon":-122.3083848,"time":1.273515839496E9,"source":"skyhook","nap":28,"altitude":0,"name":"location","hpe" :29,"bearing":0,"ncell":0,"lat":47.6544453}

I've run it through this command:
grep skyhook log-2010-05-10_18-17-28.json | cut -d"," -f1,10

to get this:

{"lon":-122.30872015,"lat":47.65812201}
{"lon":-122.3076686,"lat":47.6569975}
{"lon":-122.3079161,"lat":47.656395}
{"lon":-122.3096168,"lat":47.656218}
{"lon":-122.3096285,"lat":47.656206}

Which is a lot nicer, but I would prefer not to have to hand remove the non-number characters by hand since there are thousands of data points.

Any recommendations on what I could do to get it to just be longitude and latitude in 'number number' format?
 
Old 05-10-2010, 10:17 PM   #2
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,817

Rep: Reputation: 609Reputation: 609Reputation: 609Reputation: 609Reputation: 609Reputation: 609
grep skyhook log-2010-05-10_18-17-28.jso | cut -d"," -f1,10 | sed -e 's/{\"lon\"://g' -e 's/\"lat\"://g' -e 's/}//g'

removes {"lon":, "lat":, and }
 
Old 05-10-2010, 10:42 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,425

Rep: Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826
Could use this on original file:
Code:
awk -F, '/skyhook/{gsub(/.*:/,"",$1);gsub(/.*:|\}/,"",$NF);print $1","$NF}' log-2010-05-10_18-17-28.json
 
Old 05-10-2010, 10:45 PM   #4
stodds
LQ Newbie
 
Registered: May 2010
Posts: 6

Original Poster
Rep: Reputation: 0
Excellent, I just realized two things I hadn't seen before. One is that instead of a comma I would need a space (figured that out on my own). The second that has me head scratching is that while they are listed in longitude latitude order in the data set, I need them in latitude longitude for the final input.

so currently my command looks like this:

grep skyhook log-2010-05-10_18-17-28.json | cut -d"," -f1,10 | sed -e 's/{\"lon\"://g' -e 's/\"lat\"://g' -e 's/}//g' | tr , \

and outputs:
...
-122.3096168 47.656218
-122.3096285 47.656206
-122.3087783 47.6550685
-122.3087269 47.6547688
-122.3083848 47.6544453
...


but I would like to switch the order of the numbers if possible... any ideas?
 
Old 05-10-2010, 10:51 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Quote:
Originally Posted by stodds View Post
but I would like to switch the order of the numbers if possible... any ideas?
First, use AWK with "," as separator, and print fields $1 and $10, but in reverse order.

Then used SED to strip off the "un-numbers".
 
Old 05-10-2010, 10:53 PM   #6
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,817

Rep: Reputation: 609Reputation: 609Reputation: 609Reputation: 609Reputation: 609Reputation: 609
... | awk '{print $2" "$1 }'
 
Old 05-10-2010, 10:55 PM   #7
stodds
LQ Newbie
 
Registered: May 2010
Posts: 6

Original Poster
Rep: Reputation: 0
Excellent, Thanks a ton you guys.
 
Old 05-10-2010, 11:08 PM   #8
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 2,517
Blog Entries: 1

Rep: Reputation: 494Reputation: 494Reputation: 494Reputation: 494Reputation: 494
{"lon":-122.30872015,"lat":47.65812201}
{"lon":-122.3076686,"lat":47.6569975}
{"lon":-122.3079161,"lat":47.656395}
{"lon":-122.3096168,"lat":47.656218}
{"lon":-122.3096285,"lat":47.656206}

I was about to say, just use sed to replace "\:" with " " and "\," with " " and "\}" with " "

| sed 's/\:/\ /g' | sed 's/\,/\ /' | sed 's/\}/\ /g'

then use awk.

| awk '{ print $2 " " $4 }'

lots of ways to skin a cat. Some probably shorter / faster than the above. But I like to dumb it down. Kind of hard to debug if it's all or nothing.
 
Old 05-10-2010, 11:21 PM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,425

Rep: Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826
Yeah you can just change the order of the past two items in my post #3
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Sed append text to end of line if line contains specific text? How can this be done? helptonewbie Linux - Newbie 4 10-23-2013 02:48 PM
[SOLVED] Trying to number every other line and append those numbers to end of line kmkocot Programming 7 04-23-2010 12:17 PM
C++ text file line by line/each line to string/array Dimitris Programming 15 03-11-2008 09:22 AM
Grep's line numbers parsed into one line of output. judgex Programming 8 08-14-2006 05:22 AM


All times are GMT -5. The time now is 04:31 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration