Reputation: 414
There is txt file-:
file1-
abhinav,Age_10,11,12,13,14,15
deepak,Age_10,11,12,13,14,15
file2-:
Dixit,15
Skoda,15
Shell script-:
old_count=`grep 'abhinav' | awk 'BEGIN { FS = "," } ; { print $2 }' | awk 'BEGIN { FS = "_" } ; { print $2 }'`
new_count=`grep 'dixit' | awk 'BEGIN { FS = "," } ; { print $2 }'`
sum=`expr $old_count + $new_count`
But when this script is executed than error expr: non-numeric argument is coming . Though both variable $old_count $new_count are numeric.
Upvotes: 1
Views: 4601
Reputation: 532053
Sidestepping whatever the problem actually is, it's simpler to write this as
old_count=$( awk -F, '/abhinav/ {split($2, a, "_"); print a[2]}' file1 )
new_count=$( awk -F, '/Dixit/ {print $2}' file2 )
sum=$(( old_count + new_count ))
One issue might have been that you were grepping for dixit
, not Dixit
, resulting in an empty value for new_count
. I get a different error, but that could be based on the implementation of expr
. Note that expr
is no longer necessary for arithmetic in the shell; $((...))
should be available in any POSIX-compliant shell.
Upvotes: 1