Check if the value is Number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check if the value is Number
# 8  
Old 07-08-2010
Quote:
Originally Posted by anchal_khare
Or, may be with regex?

Code:
awk '$1 !~ /^[0-9]+\.?[0-9]+$/ {print "Error in field 1|" $0}' file1.txt

This gives also an error if the value of the field is a zero (0).
These 2 Users Gave Thanks to Franklin52 For This Post:
# 9  
Old 07-08-2010
@Franklin:

Thanks for pointing that out!!

I was under the impression that it will not throw an error if its zero. I still dont understand why it is not recognizing 0 as a valid number.Can you explain why it is behaving like that??
# 10  
Old 07-08-2010
Quote:
Originally Posted by ashwin3086
@Franklin:

Thanks for pointing that out!!

I was under the impression that it will not throw an error if its zero. I still dont understand why it is not recognizing 0 as a valid number.Can you explain why it is behaving like that??
Not only a zero but all the values with one number (0-9) gives an error, + matches at least 1 or more numbers:
Code:
^[0-9]+

# 11  
Old 07-09-2010
Quote:
Originally Posted by Franklin52
Not only a zero but all the values with one number (0-9) gives an error, + matches at least 1 or more numbers:
Code:
^[0-9]+

Yes, It wasn't a good try. Thank you for pointing.

@ashwin3086: regarding the explanation of the regular expression, I was looking for "single or more digit followed by optional dot, followed by single or more digit."

So, you can see with the description, that at least two digit must occur to satisfy the condition.

@Franklin52: to be specific, do you think the following would work?

Code:
awk '$1 !~ /^[0-9]+\.?[0-9]*$/ {print "Error in field 1|" $0}'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to check if a number exists?

Hello, May i please know how do i check if the given input argument is one of the listed numbers then success else failure. I am using bash shell. if then echo "success" else echo "failure" fi Thank you. (2 Replies)
Discussion started by: Ariean
2 Replies

2. Shell Programming and Scripting

How to check number of group of file.?

Hi Gurus, I need check existing number of file based on the list in file list. for example: in my file list. I have below: abc, file1.txt abc, file2.txt abc, file3.txt abc, file4.txt cde, filea1.txt cde, filea2.txt cde, filea3.txt ... in my current file direcotry, I have file:... (0 Replies)
Discussion started by: ken6503
0 Replies

3. Shell Programming and Scripting

number of instance check

Hi, We are getting a curios result in one of AIX script. Its executed using !/bin/ksh . After following line we get result of 3 in in the variable instance_count. instance_count=`ps -ef | grep "script_check_instances.sh" | grep -v "grep" | wc -l` But once we do a "ps -aef | grep... (2 Replies)
Discussion started by: niba
2 Replies

4. Shell Programming and Scripting

Check params for number

I have 2 and three params, both I should make sure thay numbers at one single line insted of checking for each one . Example I wroote the following way.. checking for 2 and three seperately but I shud be able to do it at on statement echo $2 | egrep '^+$' >/dev/null 2>&1 if ; then echo... (2 Replies)
Discussion started by: raopatwari
2 Replies

5. Shell Programming and Scripting

TCSH Check if number is even

Hey, I am trying to check if an integer is even in a tcsh script This is what I am running now set lattest = ` echo $latmin "%2" | bc -l ` echo $lattest if ( $lattest == 0 ) then echo "min is already even" else if ( $lattest =! 0 ) then set latmin = ` echo $latmin "+1" |... (2 Replies)
Discussion started by: travish12
2 Replies

6. Shell Programming and Scripting

To check a word is number or not

Hi, I have one file like 00123. And this file name is generated as a sequence. So how can I confirm the generated file name is a number, not a special character or alphabets. Can anybody help me out. Thanks in advance. (3 Replies)
Discussion started by: Kattoor
3 Replies

7. Shell Programming and Scripting

check number of character

hi, I would like to calculate number of character for a number, for exemple : 1200 --> there are 4 characters , 120001 -> 5 characters (4 Replies)
Discussion started by: francis_tom
4 Replies

8. Shell Programming and Scripting

number check in perl

Hi,, this is returning true in all cases..( other than 10 dig number also) what could be wrong?? (2 Replies)
Discussion started by: shellwell
2 Replies

9. Shell Programming and Scripting

How to check whether a string is number or not

Hi , I am facing a problem .. which looks simple... but took 2 days of mine.. even now it is not solved completely.. I have one variable..., want to know whether that variable contains number... canbe +ve or -ve ... Values +35 --- number -43 --- number 45A -- non number... (12 Replies)
Discussion started by: shihabvk
12 Replies

10. UNIX for Dummies Questions & Answers

Check if variable is a number

If I have a variable $X, how do I check it is a number? Many thanks. (2 Replies)
Discussion started by: handak9
2 Replies
Login or Register to Ask a Question