Test argument error, unable to solve

Here is an awk statement i am using to sum a series of numbers..

awk  -F"," '{ for (i=1; i<=NF ; ++i) sum += $i;} #  if (i > max) max=i }
END { s=""; for (i=1; i<=max; ++i) { printf "%s%s", s, sum; s=","; } printf "\n" }' filename.csv

It works fine for the summing part. But since my series contains several N/A's also along with the numbers i am getting an test argument expected error...

Is there any way of getting past this without actually removing the N/A from the inputs..?

eg input -

23, 89, N/A, 75, 89.....

You could check if the field is not a number or if it is the string N/A and then decide if you sum it up or skip it, about here:

awk  -F"," '{ for(i=1; i<=NF ; ++i) { if (i !~ /N\/A/) {sum+=$i}}}
..

Firstly thanks for the quick reply.
Your solution makes sense, but somehow i'm unable to get it to work.
Any other means, can i set the value of N/A to zero or something like that?

---------- Post updated at 05:28 PM ---------- Previous update was at 05:19 PM ----------

Or should the condition be checked inside the for loop also? Is that giving me the problem?

awk  -F"," '{ for (i=1; i<=NF ; ++i) sum += $i;} 

awk  -F"," '{ for(i=1; i<=NF ; ++i) { if ($i !~ /N\/A/) {sum+=$i}}}
1 Like

No, still not working,... This has taken me all day... :frowning:

Here is the error

N/A,: unknown test operator

I even tried this.

awk  -F"," '{ for(i=1; (i<=NF) && (i !~ /N\/A/) ; ++i)  {sum+=$i}}

@pravin27:
Thanks for pointing out the missing $.

@pravsripad:
That single line of code works; maybe you got a syntax problem?

echo "1,5,N/A,10,4"| awk  -F"," '{ for(i=1; i<=NF ; ++i) { if ($i !~ /N\/A/) {sum += $i}}} END{print sum}'
20

Hi.

Is that even an awk error?

The only place I could find it is on ksh on Solaris (maybe not a scientific method :)...):

# strings $(which ksh) | grep test\ operator
unknown test operator

# strings $(which awk) | grep test\ operator
#
# strings $(which nawk) | grep test\ operator
#
# strings /usr/xpg4/bin/awk | grep test\ operator
#

Perhaps you could show the code around your awk, and more about the error output (line no., etc.)?

Please show sample input and sample output.
May be being thick but I can't see what the array sum [i]is for.

Hi scottn, yes you're right, its not an awk error. I'm using this statement in a ksh script on solaris... :slight_smile:

I'm basically trying to send in a .csv file full of numbers and N/A's and getting the sum appended to it..

awk  -F"," '{for (i=1;i<=NF ; ++i) { if($i !~ /N\/A/) {sum += $i;}} if (i > max) max=i }
    END { s=""; for (i=1; i<=max; ++i) { printf "%s%s", s, sum; s=",";} printf "\n" }' format.csv > ffname
    cat format.csv

format.csv is something like this

20100803,N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, , 
20100804, 183,  234,  29,  31,  0,  0,  0,  0,  25,  127,  6,  21,  285,  49,  0,  0,  0,  0,  217,  2,  27,  0,  968,  332, 
20100802,N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, , 
20100803,N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, , 

I'm adding the columns corresponding to the dates...

so my result should be :-

183,  234,  29,  31,  0,  0,  0,  0,  25,  127,  6,  21,  285,  49,  0,  0,  0,  0,  217,  2,  27,  0,  968,  332,

I'm getting the result but with this error on the console, which i don't want...

finale4[71]: N/A,: unknown test operator

---------- Post updated at 09:43 PM ---------- Previous update was at 09:40 PM ----------

@methyl : - sum [i]is the array that holds my column sum...

Hi.

You probably want to change the for(i=1;... to for(i=2;... to exclude those large date numbers at the start.

Apart from that, from the code you show (none of which is ksh), this work fine in Solaris (10):

$ ./SumUp
183,234,29,31,0,0,0,0,25,127,6,21,285,49,0,0,0,0,217,2,27,0,968,332,0,0,

What is at "line 71"?

Add:

set -x

at the top of your script (after the shebang, if you have one) and post the output.

Thanks. Didn't realise you were summing columns not rows. I think one or two other posters made the same assumption.

This error message comes from shell not awk. The "71" is a line number very near to the error within a shell script called "finale4".

---------- Post updated at 17:34 ---------- Previous update was at 17:25 ----------

We can generate a similar error using a test script with the same name as yours.

#!/bin/ksh
if [ "abc" N/A, "abc" ]
then
        echo "ok"
fi

./finale4[2]: N/A,: unknown test operator

it's quite a long piece of code. I've used the awk commands in ksh. Tried with (for i=2..) but in vain...

set -x gave me the following output..

+ writer_reader
finale4[89]: N/A,: unknown test operator
finale4[71]: N/A,: unknown test operator
finale4[71]: N/A,: unknown test operator
finale4[71]: N/A,: unknown test operator
finale4[71]: N/A,: unknown test operator
finale4[71]: N/A,: unknown test operator
finale4[71]: N/A,: unknown test operator
finale4[71]: N/A,: unknown test operator
finale4[71]: N/A,: unknown test operator
finale4[71]: N/A,: unknown test operator
finale4[71]: N/A,: unknown test operator
finale4[71]: N/A,: unknown test operator
finale4[71]: N/A,: unknown test operator
finale4[71]: N/A,: unknown test operator
finale4[71]: N/A,: unknown test operator
finale4[71]: N/A,: unknown test operator
finale4[71]: N/A,: unknown test operator
finale4[71]: N/A,: unknown test operator
finale4[71]: N/A,: unknown test operator
finale4[71]: N/A,: unknown test operator
finale4[71]: N/A,: unknown test operator
finale4[71]: N/A,: unknown test operator
+ [ 1 -lt 1 ]
+ exit

writer_reader is the name of the function where this command is used. I've put only a part of the console o/p above (the one that concerns the error).

---------- Post updated at 01:45 PM ---------- Previous update was at 10:59 AM ----------

what does the number in the [] bracket indicate exactly? It is not the line number, of that i am sure. Then what is it and how do i identify the location of the error?

It is (albeit sometimes incredibly vaguely) related to the error - quite often the last point at which it doesn't make any sense to continue.

I don't really want to spend my day guessing, and second-guessing where your problem might be.

So at this point I would suggest that you post your script (at least the first 100 lines of it).

Hi, the script is 1000 line huge... The error is definitely coming from those three lines... Could it be that the "," is also getting input along with the "N/A" .... i mean the "N/A," will it matter.. ?
Tried but didn't work.

Try to run the below command at command prompt and let us know output. Its running fine at my end

[root@TEST ~]# awk  -F"," '{for (i=2;i<=NF ; ++i) { if($i !~ /N\/A/) {sum += $i;}} if (i > max) max=i }
    END { s=""; for (i=2; i<=max; ++i) { printf "%s%s", s, sum; s=",";} printf "\n" }' format.csv
183,234,29,31,0,0,0,0,25,127,6,21,285,49,0,0,0,0,217,2,27,0,968,332,0,0,

[root@TEST ~]# cat format.csv
20100803,N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, ,
20100804, 183,  234,  29,  31,  0,  0,  0,  0,  25,  127,  6,  21,  285,  49,  0,  0,  0,  0,  217,  2,  27,  0,  968,  332,
20100802,N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, ,
20100803,N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, ,

Yep, its working good on the console. The logic works in the script too and the output is alright, but i'm unable to avoid this error. :confused:

Interestingly we can get the same ksh error message by feeding an entire sample record to an if statement as if we had presented it in a text variable which was not surrounded by quotes.
What surprised me is that ksh continues running.

#!/bin/ksh
if [ 20100802,N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A, , ]
then
   echo true
else
   echo false
fi


./testscript[2]: N/A,: unknown test operator
false

If you run your script with "set -x" please show us say 50 lines of trace up to the shell error message. It should demonstrate which command is going wrong and with what values.

interesting observation there methyl, i'll re-check my if statements...

here's the console o/p

+ reader_function 06 2010 # function that reads i/p file
+ [ 25 -ne 0 ]
+ writer_reader #function that calculates sum and write o/p file
finale[89]: N/A,: unknown test operator
finale[71]: N/A,: unknown test operator
finale[71]: N/A,: unknown test operator
finale[71]: N/A,: unknown test operator
finale[71]: N/A,: unknown test operator
finale[71]: N/A,: unknown test operator
finale[71]: N/A,: unknown test operator
finale[71]: N/A,: unknown test operator
+ [ 1 -lt 2 ]
+ client=name
+ + expr 1 + 1
p=2
+ fullname=nameadm
+ echo  the Name of the Client chosen is name 
+ 1>> /fs/home-1/u1/sprav/reports/log.txt
+ reader_function 06 2010
+ [ 24 -ne 0 ]
+ writer_reader
finale[71]: N/A,: unknown test operator
finale[71]: N/A,: unknown test operator
finale[71]: N/A,: unknown test operator
finale[71]: N/A,: unknown test operator
finale[71]: N/A,: unknown test operator
finale[71]: N/A,: unknown test operator
finale[71]: N/A,: unknown test operator
finale[71]: N/A,: unknown test operator
finale[71]: N/A,: unknown test operator
finale[71]: N/A,: unknown test operator
finale[71]: N/A,: unknown test operator
finale[71]: N/A,: unknown test operator
finale[71]: N/A,: unknown test operator
finale[71]: N/A,: unknown test operator
finale[71]: N/A,: unknown test operator
finale[71]: N/A,: unknown test operator
finale[71]: N/A,: unknown test operator
finale[71]: N/A,: unknown test operator
finale[71]: N/A,: unknown test operator
finale[71]: N/A,: unknown test operator
finale[71]: N/A,: unknown test operator
finale[71]: N/A,: unknown test operator
+ [ 2 -lt 2 ]
+ rm cfile
+ rm /fs/home-1/u1/sprav/tempo/days
+ rm /fs/home-1/u1/sprav/tempo/day
+ rm header
+ rm headf
+ rm headv
+ rm tailf
+ exit

The problem is clearly in a script called "finale" but the "set -x" does not appear to be active in that script.

Can you tell me a workaround to not display this error on the console?

Redirecting stderr for this particular part of the script only?

2> 

is for the entire script...