Getting awk Output as number instead of String

Hi All,

I have a file a.txt, content as mentioned below:

 
 22454750
 
This data in this control file and

I have a variable called vCount which contains a number.

I need to extract the 22454750 from the above file and compare with the variable vCount. If match fine or else exit.

i used awk to extract the number as mentioned below:

fileCount=$(awk 'NR==1 {print $1}' a.txt)
 
if [[ $vCount == $fileCount ]] ; then
echo "match"
else "do not match"
exit 1
fi

Value of vCount is 22454750
I got msg do not match and error non numeric comparison.

Can you guys please help on the above issue . It seems the fileCount is a string. How do i convert it to number?

Thanks
Arun

try below if block

 
if [ "$vCount" = "$fileCount" ]

Hi,

Thanks for the reply. But after using the above if block i still got error.
I saw in debug mode the fileCount value as mentioned below:

 
fileCount=$'22454750\r'

the if condition is unable to match as below:

 
[' 22454750 = $'22454750\r' ']

PLease provide your inputs/suggestion on the above

You have to correct the source from which you are populating the data in the variables.. in one variable you are getting space and in another extra \r

Math tests use "-eq" "-gt" "-lt" not "=" so try one of those combinations...

Hi Arun,
If you can't (or don't want to) correct your input source, you could try one of the following:

#!/bin/ksh
vCount="$1"
echo 'Using awk:'
fileCount=$(awk '{gsub(/\r/, "")}
        NR==1 {print $1}' a.txt)
if [ "$vCount" -eq "$fileCount" ]
then    printf "\"%s\" is equal to \"%s\"\n" "$vCount" "$fileCount"
else    printf "\"%s\" is not equal to \"%s\"\n" "$vCount" "$fileCount"
fi
echo 'Using read and variable expansion:'
IFS='' read -r fileCount < a.txt
fileCount="${fileCount%%[!0-9]*}"
if [ "$vCount" -eq "$fileCount" ]
then    printf "\"%s\" is equal to \"%s\"\n" "$vCount" "$fileCount"
else    printf "\"%s\" is not equal to \"%s\"\n" "$vCount" "$fileCount"
fi

When a.txt contains a <carriage-return> as well as a <newline> as shown by this output from from the command:

od -c a.txt
0000000    2   2   4   5   4   7   5   0  \r  \n                        
0000012

then running the above script with the argument 22454750 produces the output:

Using awk:
"22454750" is equal to "22454750"
Using read and variable expansion:
"22454750" is equal to "22454750"

As you can tell from the script, I usually use the Korn shell; but this script will work with any POSIX conforming shell (including both ksh and bash).

As shamrock said, when you're comparing arithmetic values -le must be used instead of <= and -ge must be used instead of >= . With inequality, it doesn't make much difference whether you use != or -ne although -ne is preferred if you are comparing numbers. For equality, the form of test command determines the operator to use when you're comparing strings:

  1. = for test expr or [ expr ]
  2. == for [[ expr ]]

and when you're comparing numbers:

  1. -eq or = for test expr or [ expr ]
  2. -eq or == for [[ expr ]]

again with -eq being the preferred operator.