"Simple" echo/reading variable question...

Hello, I have a simple(I think) question!
Although simple, I have been unable to resolve it, so I hope someone can help! OK, here it is:

1)I have an awk script that prints something, such as:

awk '{print $2}' a > x

so x might hold the value of say '10'

2)Now, I just want to check to see if this value is greater than 0

if [[ ($x -gt 0) ]]

however, the above statement does not work!
Similarily, when I type:

echo $x 

I just get a blank line! How do I get $x to actually read my value of x? Thanks!!

x=$(awk '{print $2}' a)
echo $x
1 Like

Much thanks!
:b:

external utilities like awk/cut/sed are efficient when run on batches of data, but using them to operate on single lines is extremely wasteful. They take time to load and quit; imagine only being able to say one word per phone call.

To read and tokenize input from a file without awk, you can use the read builtin, which will be literally hundreds of times faster.

read A B C < file

which will attempt to split the line into at least three tokens. The line 'a b c d e f g' would get split into A='a', B='b', C='c d e f g'.

1 Like

Corona, thank you very much! That's very useful :slight_smile: