LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   ambiguous output using variables in bash (https://www.linuxquestions.org/questions/linux-newbie-8/ambiguous-output-using-variables-in-bash-551101/)

gwong 05-03-2007 05:21 PM

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

Tinkster 05-03-2007 07:03 PM

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

jaykup 05-05-2007 08:49 PM

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'


All times are GMT -5. The time now is 09:00 AM.