Sponsored Content
Full Discussion: Check if the value is Number
Top Forums Shell Programming and Scripting Check if the value is Number Post 302435782 by radoulov on Thursday 8th of July 2010 07:51:58 AM
Old 07-08-2010
Quote:
Originally Posted by Franklin52
Or may be with:
Code:
awk '!($0+0==$0){print $0 " not a number"}' file

Yep,
this should be fine Smilie

---------- Post updated at 01:51 PM ---------- Previous update was at 01:48 PM ----------

Quote:
Originally Posted by ashwin3086
Thanks for the code in perl!!
Is it possible in Unix??

I might have multiple Number fields like field 1 ,field 3, etc. I need to check for all fields at once without reading the file line by line.
You cannot do this without reading the file (line by line or all at once).

Using Franklin52's idea:

Code:
awk -F\| '{
  for (i = 0; ++i <= NF;)
    if (!isnum($i))
	  printf "line %d, field %i does\47t look like a number: %s\n", \
	    NR, i, $i
  }
func isnum(x) {
  return x == x + 0
  }' infile

Code:
% cat infile 
123|123
234|234
23e|23e
234.456|234.456
234.876e|234.876e
345.00|345.00
% awk -F\| '{
  for (i = 0; ++i <= NF;)
    if (!isnum($i))
  printf "line %d, field %i does\47t look like a number: %s\n", \
    NR, i, $i
  }
func isnum(x) {
  return x == x + 0
  }' infile
line 3, field 1 does't look like a number: 23e
line 3, field 2 does't look like a number: 23e
line 5, field 1 does't look like a number: 234.876e
line 5, field 2 does't look like a number: 234.876e

Or if you're only interested of lines with only valid numbers:

Code:
awk -F\| '{
  for (i = 0; ++i <= NF;)
    if (!isnum($i)) {
	  print "line", NR, "contains invalid numbers, skipping it ..."
	  break
	  }
  }
func isnum(x) {
  return x == x + 0
  }' infile

Which with the above sample data yields:

Code:
line 3 contains invalid numbers, skipping it ...
line 5 contains invalid numbers, skipping it ...


Last edited by radoulov; 07-08-2010 at 09:06 AM..
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 10:45 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy