1753449 Members
6135 Online
108794 Solutions
New Discussion юеВ

Re: Awk in Shell

 
Arun Kumar Rajamari
Frequent Advisor

Awk in Shell

Hi,
The following is a part of my shell script.
----------------------------------------------
echo "$value $value2"|awk '
{
if ($1 <= $2)
system($command);
else
print 0;
}'
----------------------------------------------
where I have assigned some command to $command.

While running the script I am getting the error like
----------------------------------------------
Value1 is: 903824
Value2 is: 2456910.90
903824 2456910.90
awk: Field $() is not correct.
The input line number is 1.
The source line number is 4.
----------------------------------------------
But if i change the if condition to $1>=$2,

I am getting the output like,
------------------------
Value1 is: 903837
Value2 is: 2456972.10
903837 2456972.10
0
--------------------------
Is there any other way to compare 2 floating values other than the above.

Could you please clarify me in the above and to rectify my errors

Thanks,
Arun
4 REPLIES 4
Dennis Handly
Acclaimed Contributor

Re: Awk in Shell

>where I have assigned some command to $command.

Since your awk script is in '', $variables aren't expanded and they are treated as awk fields.

You will have to change the script to:

echo "$value $value2"|awk -v command="$command" '
{...
system(command);
Peter Nikitka
Honored Contributor

Re: Awk in Shell

Hi,

another possibilities to give awk the content of a shell variable:
1)
echo "$value $value2"|awk '
{
if ($1 <= $2)
system("'"$command"'");
else
print 0;
}'

2)
export command
echo "$value $value2"|awk '
{
if ($1 <= $2)
system(ENVIRON["command"]);
else
print 0;
}'

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Hein van den Heuvel
Honored Contributor

Re: Awk in Shell

Hmmm, the assigned points (5) suggest the problem is not solved, but there is no indication on what is (still) wrong.

I have no HPUX system up right now to test, so I tried on Windoze..

- Are you sure ydeou want those quotes around the values? On Windows they ended up in $1 and $2. To test replace the system call with 'print "1:", $1, "2:", $2'.

- Clean up the if-then-else.
Use parantheses:

if ($1 <= $2) {
system($command);
} else {
print 0;
}

- As replied... the $command substitution will not work. You'll need to feed it through ENVIRON or a -v, or as still more text to that echo:

echo "$v1 $v2 $command" | ....
...system ("$3 $4 $5...)"...

hth,
Hein.
Arun Kumar Rajamari
Frequent Advisor

Re: Awk in Shell

Followed the above commands and got it worked
Thanks,
Arun