Languages and Scripting

How to check whether a field is numeric

 
Pavitra
Occasional Advisor

How to check whether a field is numeric

Hi all,
i have a file containing comma separated fields,
i want to check whether the field 4 and field 5 is numeric or not
(not in perl pls)
10 REPLIES 10
Orhan Biyiklioglu
Respected Contributor

Re: How to check whether a field is numeric


awk -F, '($4 ~ /^[0-9]+$/) && ($5 ~/^[0-9]+$/){print $0}' filename

will print the lines with numeric fields on columns 4 and 5.

hth
Peter Godron
Honored Contributor

Re: How to check whether a field is numeric

Pavitra,
this is NOT the short way of doing it, but it is pretty self-explanatory:

#!/usr/bin/sh
while read record
do
field4=`echo $record | awk -F',' '{print $4}' | tr -d [:digit:]`
field5=`echo $record | awk -F',' '{print $5}' | tr -d [:digit:]`
if [ -z "$field4" ]
then
echo "Field 4 is numeric"
fi
if [ -z "$field5" ]
then
echo "Field 5 is numeric"
fi
done < data.lis

Assumtions:
integers only
Pavitra
Occasional Advisor

Re: How to check whether a field is numeric

hey thanks a lot
but..
i want to print the field number and also record number
if the 4th and 5th field is not numeric
pls help
Peter Godron
Honored Contributor

Re: How to check whether a field is numeric

Hi,
modified for record count etc.

#!/usr/bin/sh
count=0
while read record
do
count=`expr $count + 1`
field4=`echo $record | awk -F',' '{print $4}' | tr -d [:digit:]`
field5=`echo $record | awk -F',' '{print $5}' | tr -d [:digit:]`
if [ -n "$field4" ] -o [ -n "$field5" ]
then
echo "In record $count Field 4 [ $field4 ] or Field 5 [ $field5 ]is not numeric"
fi
done <
Orhan Biyiklioglu
Respected Contributor

Re: How to check whether a field is numeric

Yet another one liner:

awk -F, '($4 ~ /^[0-9]+$/) && ($5 ~/^[0-9]+$/){print FNR," fields 4 and 5 are not numeric!"}' filename


hth
Pavitra
Occasional Advisor

Re: How to check whether a field is numeric

i have attached the file
i want the field no and record no
eg: print (NR,i)
Arturo Galbiati
Esteemed Contributor

Re: How to check whether a field is numeric

Hi, base on your file, please show the output you need to better udnerstand your request. If I weel understood you want the record number and the filed number of those record where OR F4 OR F5 OR both are not numeric. Is it correct?
Rgds,
Art
Hein van den Heuvel
Honored Contributor

Re: How to check whether a field is numeric

Good question Arturo!

Can there be comma's between the quotes?

Check out:
https://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=1064324


>>> (not in perl pls)

I always find that an intesting comment.
This is a relatively simple task, and very simple in perl. Apparently you do not know how to solve this yourself, so why would it matter what implementation language the solution would be in? Once you have the framwork, you'll will surely be able to figure out how to modify & maintain after minimal studying no matter what language!

Regards,
Hein.

perl -lne '@F=split /,/; for $i (4,5) { print qq(Line $. Field $i not numeric) if $F[$i-1] =~ /\D/}' tmp.txt
Line 3 Field 4 not numeric
Line 5 Field 5 not numeric

-l # l=new-line with prin
-n # loop no print
-e # program text follows

@F=split /,/; # split currenlt line ($_) into array F using commans as field delimitor

for $i (4,5) # list of fields to check
print qq( # Alternative for quoted string
$. $i # current line number and field
$F[$i-1] # array element for field number
=~ /\D/ # string match for non-decimal (\d = decimal)

Arturo Galbiati
Esteemed Contributor

Re: How to check whether a field is numeric

Hi,
based on this file:
/> cat f2
"ABC","RRRRR","AAAA",009,999
"ABC","RRRRR","AAAA",009,999
"PPC","PPPPP","AAA",X,1
"PPC","PPPPP","AAA",1,X
"PPC","PPPPP","AAA",Y,X

and using this:
awk -F, '{if ($4 ~ /^[0-9]+$/) F4=4 ;else F4=" "
if ($5 ~ /^[0-9]+$/) F5=5 ;else F5=" "
print NR " " F4 " " F5}
' f2

you get:
1 4 5
2 4 5
3 5
4 4
5

is it ok?
Rgds,
Art