Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 05-03-2007, 05:21 PM   #1
LQ Newbie
Registered: May 2007
Posts: 1

Rep: Reputation: 0
ambiguous output using variables in bash


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


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`)


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, 07:03 PM   #2
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911

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?

Old 05-05-2007, 08:49 PM   #3
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:
for x in *.kml
     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"
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.

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

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


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

All times are GMT -5. The time now is 09:50 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration