- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- How to check whether a field is numeric
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-13-2007 08:16 PM
тАО02-13-2007 08:16 PM
How to check whether a field is numeric
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)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-13-2007 08:32 PM
тАО02-13-2007 08:32 PM
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
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-13-2007 08:38 PM
тАО02-13-2007 08:38 PM
Re: How to check whether a field is numeric
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-13-2007 08:45 PM
тАО02-13-2007 08:45 PM
Re: How to check whether a field is numeric
but..
i want to print the field number and also record number
if the 4th and 5th field is not numeric
pls help
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-13-2007 08:51 PM
тАО02-13-2007 08:51 PM
Re: How to check whether a field is numeric
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 <
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-13-2007 09:09 PM
тАО02-13-2007 09:09 PM
Re: How to check whether a field is numeric
awk -F, '($4 ~ /^[0-9]+$/) && ($5 ~/^[0-9]+$/){print FNR," fields 4 and 5 are not numeric!"}' filename
hth
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-13-2007 09:21 PM
тАО02-13-2007 09:21 PM
Re: How to check whether a field is numeric
i want the field no and record no
eg: print (NR,i)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-13-2007 09:56 PM
тАО02-13-2007 09:56 PM
Re: How to check whether a field is numeric
Rgds,
Art
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-13-2007 11:17 PM
тАО02-13-2007 11:17 PM
Re: How to check whether a field is numeric
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)
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-13-2007 11:42 PM
тАО02-13-2007 11:42 PM
Re: How to check whether a field is numeric
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