1 |
----- Original Message ----- |
2 |
From: "Alex Schuster" <wonko@×××××××××.org> |
3 |
To: <gentoo-user@l.g.o> |
4 |
Sent: Tuesday, August 11, 2009 4:59 AM |
5 |
Subject: Re: [gentoo-user] Bash script inquiry |
6 |
|
7 |
|
8 |
> Richard Marza writes: |
9 |
> |
10 |
>> FILE=`cat filename.txt` |
11 |
>> TICK=`cat filename.txt | wc -l' |
12 |
>> TOCK="0" |
13 |
>> |
14 |
>> while [ $TICK != $TOCK ] ; do |
15 |
>> let $TOCK=$TOCK+1 |
16 |
> |
17 |
> Or, simpler, as we are using bash: (( TOCK++ )) |
18 |
> |
19 |
>> Var1= `cat FirstWordOfFirstColumnOfFirstLine` (This I |
20 |
>> actually achieved with sed and awk) |
21 |
>> Var2=`cat FirstFloatOfFirstLine` (The problem lies here; |
22 |
>> it's my inability to come up with a way of implementing a variable that |
23 |
>> changes along with the counter. so that the second time this is run it |
24 |
>> doesn't do the first line but moved to the second line and the third line |
25 |
>> and so on...) |
26 |
>> |
27 |
>> done |
28 |
>> |
29 |
>> exit 0 |
30 |
> |
31 |
> What should Var1 contain - "Dbase1" or the content of the file "Dbase1"? |
32 |
> What should Var2 contain - "5.0" or the content of the file "5.0"? |
33 |
> Because you are using cat in the assignment. |
34 |
> If you just need the values in a variable, do it like this: |
35 |
> |
36 |
> file=filename.txt |
37 |
> Var1=( $( cat "$file" | awk '{ print $1 }' ) ) # creates an array variable |
38 |
> Var2=( $( cat "$file" | awk '{ print $2 }' ) ) |
39 |
> |
40 |
> The $() notation does the same as backticks, but is more readable. Using |
41 |
> foo=( ... ) will create foo as an array. I assume there is no whitespace |
42 |
> in |
43 |
> your data, that is Var1 will never contain something like "Dbase 1". |
44 |
> |
45 |
> ${#Var1[@]} will contain the number of elements (your $TICK). To access |
46 |
> the |
47 |
> 5th element (for example), use ${Var1[4]}. |
48 |
> |
49 |
> Oh, an please don't hijack threads by replying to an existing one, but |
50 |
> start |
51 |
> a new one. This one appears inside the "Cloning movie DVDs" thread. |
52 |
> |
53 |
> And feel free to ask more questions, maybe I got it all wrong. |
54 |
> |
55 |
> Wonko |
56 |
> |
57 |
|
58 |
I did not intend to hijack. Next time I will be more cautious. Your |
59 |
information is useful. I have used awk. The array is very useful here. |
60 |
|
61 |
Think of the file I'm using as a spreadsheet. The headers(column names) are |
62 |
on top and the values are below them. Each line has an item with multiple |
63 |
values under different systems. |
64 |
|
65 |
|
66 |
Item System1 System3 System4 ... |
67 |
|
68 |
nio 5.0 5.5 5.0 (these are individual |
69 |
values. They are nothing more than what they represent. The item (nio) and |
70 |
the float or integer representing its value under the different |
71 |
system...It's just a file) |
72 |
|
73 |
My goal is to take "nio" and figure out which system has a different price |
74 |
than the others. So if the script were to run through 200 lines of similar |
75 |
text it should definitely kick-out: "nio has discrepancy in System3; price |
76 |
5.5 in line 1" |
77 |
|
78 |
|
79 |
Another thing, all systems can have different prices. This must also kick |
80 |
out. This is really a script to report discrepancies. |
81 |
|
82 |
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. |