The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Parsing file, yaml file? Extracting specific sections Rhije Shell Programming and Scripting 3 01-22-2009 06:36 PM
Extracting information from Config files /text processing oconmx Shell Programming and Scripting 3 01-21-2009 07:09 PM
Extracting data from text file based on configuration set in config file suparnbector Shell Programming and Scripting 3 08-10-2007 03:25 AM
Have a shell script check for a file to exist before processing another file heprox Shell Programming and Scripting 3 11-14-2006 03:26 AM
[Splitting file] Extracting group of segments from one file to others ozgurgul Shell Programming and Scripting 1 09-14-2006 01:17 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-01-2009
Grizzly Grizzly is offline
Registered User
  
 

Join Date: Sep 2009
Posts: 28
Extracting From A File Then Processing

Hi. Im working with this data in a file:

Code:
-94.49109387652327,39.2956736296775
-93.0906917141962,38.72762798197614
-90.57659976220785,-40.25685140137304
-92.340961875134,39.44522321129584
92.340961875134,39.44522321129584
-94.72083812873272,37.63567097374739
Is there a way to set everything before the comma as say var1 and everything after the comma as var2. Then be able to process those vars, then move onto the next set until theres no more sets?

All that data was extracted from a KML using:

Code:
sed -n 's/.*<coordinates>\(.*\)\,.*/\1/ip;T' Test.kml
The line it extracts from looks like this:

Code:
<coordinates>-90.57659976220785,40.25685140137304,0</coordinates>
Maybe do the same thing only extracting them individually from the KML instead of the other file and then processing them, then moving to the next and so forth?
  #2 (permalink)  
Old 10-01-2009
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,122
Code:
nawk -F'[>,]' '{print $2, $3}' Test.kml
  #3 (permalink)  
Old 10-01-2009
scottn scottn is online now Forum Advisor  
VIP Member
  
 

Join Date: Jun 2009
Location: Zürich, CH
Posts: 1,059
Hi.

You could try something like:

Code:
sed -n 's/.*<coordinates>\(.*\)\,.*/\1/ip;T' Test.kml | while IFS=, read A B; do
  echo a is $A   b is $B
  ...
done
(this assumes that your Test.kml looks like the data you posted after sed processed it)

i.e.
Code:
-94.49109387652327,39.2956736296775
-93.0906917141962,38.72762798197614
-90.57659976220785,-40.25685140137304
-92.340961875134,39.44522321129584
92.340961875134,39.44522321129584
-94.72083812873272,37.63567097374739
Then the output is
Code:
a is -94.49109387652327 b is 39.2956736296775
a is -93.0906917141962 b is 38.72762798197614
a is -90.57659976220785 b is -40.25685140137304
a is -92.340961875134 b is 39.44522321129584
a is 92.340961875134 b is 39.44522321129584
a is -94.72083812873272 b is 37.63567097374739
  #4 (permalink)  
Old 10-02-2009
Grizzly Grizzly is offline
Registered User
  
 

Join Date: Sep 2009
Posts: 28
This is just to compile some stuff for my reference.

Code:
sed -n 's/.*<coordinates>\(.*\)\,.*/\1/ip;T' Test.kml 
-94.49109387652327,39.2956736296775
-93.0906917141962,38.72762798197614
-90.57659976220785,40.25685140137304
-92.340961875134,39.44522321129584
-92.340961875134,39.44522321129584
-94.72083812873272,37.63567097374739


sed -n 's/.*<longitude>\(.*\)<\/longitude>.*/\1/ip;T' Test.kml 
-94.49109387652327
-93.0906917141962
-90.57659976220786
-92.340961875134
-92.340961875134
-94.72083812873272

#Math with decimals
a=`echo "1+1.2" | bc` && echo $a
2.2

a=`dc -e '1.2 1+p'`
echo $a
2.2

#Extracting coords as vars then processing
sed -n 's/.*<coordinates>\(.*\)\,.*/\1/ip;T' Test.kml | while IFS=, read A B; do
  echo a is $A   b is $B
  echo "asdf"
done
a is -94.49109387652327 b is 39.2956736296775
a is -93.0906917141962 b is 38.72762798197614
a is -90.57659976220785 b is 40.25685140137304


#Multi Condition if statement
if [ $i = "+" -o $i = "-" -o $i = "/" -o $i = "%" ]; then
$x=$vr1
else
print "You have entered an invalid option."
  #5 (permalink)  
Old 10-02-2009
scottn scottn is online now Forum Advisor  
VIP Member
  
 

Join Date: Jun 2009
Location: Zürich, CH
Posts: 1,059
Quote:
Originally Posted by Grizzly View Post
This is just to compile some stuff for my reference.

Code:
sed -n 's/.*<coordinates>\(.*\)\,.*/\1/ip;T' Test.kml 
-94.49109387652327,39.2956736296775
-93.0906917141962,38.72762798197614
-90.57659976220785,40.25685140137304
-92.340961875134,39.44522321129584
-92.340961875134,39.44522321129584
-94.72083812873272,37.63567097374739


sed -n 's/.*<longitude>\(.*\)<\/longitude>.*/\1/ip;T' Test.kml 
-94.49109387652327
-93.0906917141962
-90.57659976220786
-92.340961875134
-92.340961875134
-94.72083812873272

#Math with decimals
a=`echo "1+1.2" | bc` && echo $a
2.2

a=`dc -e '1.2 1+p'`
echo $a
2.2

#Extracting coords as vars then processing
sed -n 's/.*<coordinates>\(.*\)\,.*/\1/ip;T' Test.kml | while IFS=, read A B; do
  echo a is $A   b is $B
  echo "asdf"
done
a is -94.49109387652327 b is 39.2956736296775
a is -93.0906917141962 b is 38.72762798197614
a is -90.57659976220785 b is 40.25685140137304


#Multi Condition if statement
if [ $i = "+" -o $i = "-" -o $i = "/" -o $i = "%" ]; then
$x=$vr1
else
print "You have entered an invalid option."
I never thought of the site as a personal organiser before Have a word with Neo. He might want to endorse it!
  #6 (permalink)  
Old 10-03-2009
Grizzly Grizzly is offline
Registered User
  
 

Join Date: Sep 2009
Posts: 28
Hah yea it just made sense. Easiest way to transport code from home to work. I tried out

Code:
sed -n 's/.*<coordinates>\(.*\)\,.*/\1/ip;T' Test.kml | while IFS=, read A B; do
  echo a is $A   b is $B
  echo "asdf"
done
at home on Ubuntu 9.04 and it worked but now it says that the command is garbled on solaris 10. I had a feeling this would happen as Im sure my Solaris box must be running an older version of pretty much everything. Can anyone think of a more flexible or older sed/bash friendly way perhaps?
  #7 (permalink)  
Old 10-03-2009
scottn scottn is online now Forum Advisor  
VIP Member
  
 

Join Date: Jun 2009
Location: Zürich, CH
Posts: 1,059
Hi.

Well, the nawk from vger works fine, which would leave you with:
Code:
nawk -F'[>,]' '{print $2, $3}' | while read A B; do
  echo a is $A   b is $B
done
(use /usr/xpg4/bin/awk if you don't have nawk)

Or an alternative sed:
Code:
sed 's/.*<coordinates>\(.*\)<.*/\1/'  | while IFS=, read A B junk; do
  echo a is $A   b is $B
done
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:18 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0