Getting awk Output as number instead of String


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting awk Output as number instead of String
# 1  
Old 05-13-2013
Getting awk Output as number instead of String

Hi All,

I have a file a.txt, content as mentioned below:
HTML Code:
 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:
Code:
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

Last edited by Scrutinizer; 05-13-2013 at 06:27 PM.. Reason: code tags
# 2  
Old 05-13-2013
try below if block

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

# 3  
Old 05-13-2013
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:
Code:
 
fileCount=$'22454750\r'

the if condition is unable to match as below:

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

PLease provide your inputs/suggestion on the above
# 4  
Old 05-13-2013
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
# 5  
Old 05-13-2013
Math tests use "-eq" "-gt" "-lt" not "=" so try one of those combinations...
# 6  
Old 05-13-2013
Hi Arun,
If you can't (or don't want to) correct your input source, you could try one of the following:
Code:
#!/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:
Code:
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:
Code:
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Concatenate a string and number and compare that with another string in awk script

I have below code inside my awk script if ( $0 ~ /SVC IN:/ ) { svc_in=substr( $0,23 , 3); if (msg_start == 1 && msg_end == 0) { msg_arr=$0; } } else if ( $0 ~ /^SVC OUT:/ ) { svc_out=substr( $0, 9, 3); if (msg_start == 1 && msg_end == 0) ... (6 Replies)
Discussion started by: bhagya123
6 Replies

2. Shell Programming and Scripting

Count the number of string occurrences to display 0 entries in output

Hello Friends, Can somebody assist an issue I am having? I have a separate file with a list of account ids XXX200B02Y01 XXX200B03Y01 XXX200B05Y01 XXX200B07Y01 XXX200B08Y01 I call the file, and run an egrep against a directory and logfiles AccountID=$(cat... (2 Replies)
Discussion started by: liketheshell
2 Replies

3. Shell Programming and Scripting

awk to output lines less than number

I am trying to output all lines in a file where $7 is less than 30. The below code does create a result file, but with all lines in the original file. The original file is tab deliminated is that the problem? Thank you :). awk 'BEGIN{FS=OFS=","} $7 < 30 {print}' file.txt > result.txt... (3 Replies)
Discussion started by: cmccabe
3 Replies

4. Shell Programming and Scripting

awk - find number in string

I am trying to go through a file that has a few million lines. I want to only pull lines that contain a number anywhere in the ninth field, but it has to be after a "/" character. Here is my awk: awk -F\| '$9 ~ /\/*{1,}*/ {print $0}' file1 > file2 However, it is just printing out every... (3 Replies)
Discussion started by: dagamier
3 Replies

5. Shell Programming and Scripting

awk number output

Hi, I have a problem when doing calculations in awk. I want to add up a few numbers and output the result. testfile: 48844322.87 7500.00 10577415.87 3601951.41 586877.64 1947813.89 $ awk '{x=x+$1};END{print x}' testfile 6.55659e+07The problem is the number format. It should show... (3 Replies)
Discussion started by: Subbeh
3 Replies

6. Shell Programming and Scripting

awk string to number conversion

Can someone explain whats happening here: $ awk 'BEGIN {print (2.5 - 1)}' 1,5 2.5 - 1 is correctly calculated to 1,5 (using european locale) $ echo "2.5" | awk '{temp = $1 - 1; print temp}' 1 If i now pipe the string 2.5 through awk it seems at it truncates 2.5 to 2? What's the... (4 Replies)
Discussion started by: beow
4 Replies

7. Programming

get ID number from one string name by using awk

Hi guys, I am new to unix shell scripts. I have a file-A.txt which contained several names in "ABCo12345678.gz_to_ABCn12345678.gz" format. I want to extract the numbers in a "for" loop that means I can not use cut -c6-13 A.txt.Dose anyone know how to do it by using awk? Thank you so much. ... (4 Replies)
Discussion started by: shrimpj
4 Replies

8. UNIX for Dummies Questions & Answers

AWK - number of specified characters in a string

Hello, I'm new to using AWK and would be grateful for some basic advice to get me started. I have a file consisting of 10 fields. Initially I wish to calculate the number of . , ~ and ^ characters in the 9th field ($9) of each line. This particular string also contains alphabetical... (6 Replies)
Discussion started by: Olly
6 Replies

9. Shell Programming and Scripting

awk: round output or delimit output of arithmatic string

I have a file with the following content. > cat /tmp/internetusage.txt 6709.296322 30000 2/7/2010 0.00I am using the following awk command to calculate a percentage from field 1 and 2 from the file. awk '{ print $1/$2*100 }' /tmp/internetusage.txt This outputs the value "22.3643" as a... (1 Reply)
Discussion started by: jelloir
1 Replies

10. Shell Programming and Scripting

awk/sed - getting string instead of number

Hi! I am writing a script handling downloading list of files and I have to check whether file is present locally and if not finished than continue downloading. To do so I have to compare sizes of remote file and local file. To check remote file size I have to parse something like this: ... (2 Replies)
Discussion started by: hrwath
2 Replies
Login or Register to Ask a Question