LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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-03-2007, 06:21 PM   #1
gwong
LQ Newbie
 
Registered: May 2007
Posts: 1

Rep: Reputation: 0
ambiguous output using variables in bash


Hi,

i'm trying to write a simple bash script that parses information from all the *.kml files then saving it into csv files of the same name, this is what i have

#! /bin/bash
#author G wong

file1=$1

for file in *.kml ; do

outputname=$file1 | sed 's/.kml/.csv/g'

(cat $1| grep "<coordinate" | sed 's/<coordinates>// g' | sed 's/,0<\/coordinates>// g' | sed 's/\t//g' >> `$outputname`)

shift



however, it tells me that >> $outname is ambiguous, i've tried it without the grave symbols as well but with no avail, is there a way to use variables to specify a output name? thanks in advance
 
Old 05-03-2007, 08:03 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Hi,

And welcome to LQ!

What are you trying to achieve in the first place, and what are you passing
as the parameter to the script?


Cheers,
Tink
 
Old 05-05-2007, 09:49 PM   #3
jaykup
Member
 
Registered: Jan 2006
Location: Mukwonago, WI
Distribution: Slackware 12
Posts: 77

Rep: Reputation: 16
With out anything to test it, I can only make some assumptions.

I see what you are trying to do with outputname, but try this:
Code:
for x in *.kml
do
     outputname=`echo $x | sed 's|.kml|.csv|g'`

     cat $x | grep "<coordinate" | sed 's|<coordinates>||g' | 
     sed 's|,0</coordinates>||g' sed 's|\t||g' >> "$outputname"
done
anything wrapped in `` will execute the command, and then use its value. VAR=`echo hi` is the same as VAR="hi"

You don't need to put the last $outputname in those quotes because its not a command anymore. We already executed it at the top. This will execute the command each time it loops.

I assume you want to dump all your changes into a .csv file instead of a .kml file, so in the echo command you should use $x instead of $file1 ($1) which is only one file (or maybe many with *.something, but that would be redundant), not each of the .kml files you are using.

Also, I assume you are going to want to cat each file and in the loop and save it as a .csv, you will need to use the $x instead of $1 to do that.

You can use anything in place of $x, it's just standard practice for me. $file would work too, but i don't like using it because its a command (not that it will hurt anything).

You may need "" around $x if you have problems. You can also use them around the entire echo command like this: "`echo $x | sed 's|.kml|.csv|g'`" command

Last note, you can use anything in sed for a divider, as long as its the same. Not using / helps to not have to use a \ every time you have something you need to cancel.

Code:
sed 's/\/usr\/local/\bin/\/usr\/bin/g'
can also be
Code:
sed 's|/usr/local/bin|/usr/bin|g'
or
Code:
sed 's#/usr/local/bin#/usr/bin#g'

Last edited by jaykup; 05-05-2007 at 09:54 PM.
 
  


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
bash returns ambiguous redirect with date in filename rstoutmna Programming 5 09-13-2011 08:37 PM
Help With Variables In Bash jamie_h Programming 3 11-01-2006 09:18 AM
G++ environment variables, rerouting output, file listings and PIDs Kawahee Programming 7 09-19-2005 01:40 PM
Scripting : Dual line output into next script as variables facets Programming 6 06-10-2004 03:28 PM
Ambiguous output redirect XFox Linux - Newbie 5 10-17-2003 08:29 PM


All times are GMT -5. The time now is 05:54 AM.

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