----- Original Message ----- 
From: "Alex Schuster" <wo...@wonkology.org>
To: <gentoo-user@lists.gentoo.org>
Sent: Tuesday, August 11, 2009 4:59 AM
Subject: Re: [gentoo-user] Bash script inquiry


> Richard Marza writes:
>
>> FILE=`cat filename.txt`
>> TICK=`cat filename.txt | wc -l'
>> TOCK="0"
>>
>> while [ $TICK != $TOCK ] ; do
>>             let $TOCK=$TOCK+1
>
> Or, simpler, as we are using bash: (( TOCK++ ))
>
>>             Var1= `cat FirstWordOfFirstColumnOfFirstLine` (This I
>> actually achieved with sed and awk)
>>             Var2=`cat FirstFloatOfFirstLine`   (The problem lies here;
>> it's my inability to come up with a way of implementing a variable that
>> changes along with the counter.  so that the second time this is run it
>> doesn't do the first line but moved to the second line and the third line
>> and so on...)
>>
>> done
>>
>> exit 0
>
> What should Var1 contain - "Dbase1" or the content of the file "Dbase1"?
> What should Var2 contain - "5.0"    or the content of the file "5.0"?
> Because you are using cat in the assignment.
> If you just need the values in a variable, do it like this:
>
> file=filename.txt
> Var1=( $( cat "$file" | awk '{ print $1 }' ) ) # creates an array variable
> Var2=( $( cat "$file" | awk '{ print $2 }' ) )
>
> The $() notation does the same as backticks, but is more readable. Using
> foo=( ... ) will create foo as an array. I assume there is no whitespace 
> in
> your data, that is Var1 will never contain something like "Dbase 1".
>
> ${#va...@]} will contain the number of elements (your $TICK). To access 
> the
> 5th element (for example), use ${Var1[4]}.
>
> Oh, an please don't hijack threads by replying to an existing one, but 
> start
> a new one. This one appears inside the "Cloning movie DVDs" thread.
>
> And feel free to ask more questions, maybe I got it all wrong.
>
> Wonko
>

I did not intend to hijack. Next time I will be more cautious. Your 
information is useful. I have used awk. The array is very useful here.

Think of the file I'm using as a spreadsheet. The headers(column names) are 
on top and the values are below them. Each line has an item with multiple 
values under different systems.


Item   System1 System3 System4 ...

nio        5.0        5.5        5.0            (these are individual 
values. They are nothing more than what they represent. The item (nio) and 
the float or integer representing its value under the different 
system...It's just a file)

My goal is to take "nio" and figure out which system has a different price 
than the others. So if the script were to run through 200 lines of similar 
text it should definitely kick-out: "nio has discrepancy in System3; price 
5.5 in line 1"


Another thing, all systems can have different prices. This must also kick 
out. This is really a script to report discrepancies. 

Hopefully this one does not appear to be hijacked. If it is let me know and 
I'll stop the discussion with regards to my inquiry. Thank you.

Reply via email to