Validate numerical


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Validate numerical
# 1  
Old 11-20-2014
Validate numerical

friends that way I can validate that the value is numeric taken

instance

Code:
var = 12re

if var = numeric then a
echo "Numerical
else
echo "not mumerico"
fi

Moderator's Comments:
Mod Comment edit by bakunin: please use CODE-tags for code, data and terminal output (yourself).

Last edited by bakunin; 11-21-2014 at 03:53 AM..
# 2  
Old 11-20-2014
Hello tricampeon81,

Please use code tags for commands and codes in your post as per forum rules. Following may help you in same.

Code:
awk -vvar="12re" 'BEGIN{if(var !~ /[[:alpha:]]/){print "Numerical"} else {print "NON numerical"}}'

Output is as follows.
Code:
NON numerical

EDIT: Adding one more approach a little different way.

Code:
awk -vvar="12re" 'BEGIN{var1=var !~ /[[:alpha:]]/?1:0; {if(var1){print "Numerical"} else {print "NON numerical"}}}'

Thanks,
R. Singh

Last edited by RavinderSingh13; 11-20-2014 at 11:01 AM.. Reason: Added code tags comment for user+ added one more solution
# 3  
Old 11-20-2014
is there another option that I'm inside a awk and do not function inside another awk awk
# 4  
Old 11-20-2014
Bash
Code:
#!/bin/bash

isnumeric() { test "$1" && printf '%f' "$1" >/dev/null 2>&1; }


if isnumeric 12; then
	echo "numeric"
fi

if isnumeric "12a"; then
	echo "numeric";
else
	echo "It's not numeric"
fi

# negative number
if isnumeric -12; then
	echo "numeric"
fi


awk


Code:
function isnum(x){return(x==x+0)}

Code:
awk 'function isnum(x){return(x==x+0)} BEGIN{print isnum("hello"),isnum("-42")}'

Code:
awk 'function isnum(x){return(x==x+0)} 
      BEGIN{
                if(isnum("Hellow"))
                {
                        print "number"
                }
           else{
                        print "not number"
                }
           }'


Last edited by Akshay Hegde; 11-20-2014 at 11:20 AM..
This User Gave Thanks to Akshay Hegde For This Post:
# 5  
Old 11-20-2014
add before I need to validate that either number as I can make
it is in this if


* if (substr ($ 0,1,1) == 'D' && substr ($ 0,24,1) == "C") # Full details Feed

Code:
BEGIN { 
	   #VARIABLES PARA CARGOS
	   #Awk ancargado de validar el detaale de un archivo cnotra la caberera 
	    cat=0;
		contador =0;
		cantidad_de_registros=0;
	    total_cabecera_abono=0;
		total_cabecera_cargo=0;
		
		suma_total_cargos=0;
		suma_total_abono=0;	

		
		ok_cantidad=0;
		nok_cantidad=1;
		
		ok_abono=0;
		ok_cargo=0;
		
		nok_cargo=1;
		nok_abono=1;
		valor=0;
		valor_final=0;
		ban=0;
		
	 }	
	 { #recorro archivo
	 
	 #datos de cabecera cantidad de registro
	 if (substr($0,1,1) =="C" && cat == 0)#Cantidad de registros cabecera
	  {
	   cantidad_de_registros=int(substr($0,2,8))
	  }
	  #rescato los cargos
	  if (substr($0,1,1) =="C" && cat == 0)#total cargo cabecera
	  {
	   total_cabecera_cargo=int(substr($0,24,14))
	  }
	  
	  #Datos de cabecera
	  if (substr($0,1,1) =="C" && cat == 0)#total abono cabecera
	  {
	   total_cabecera_abono=int(substr($0,10,14))
	  }
	  
	  #Datos de Detalle
	    if (substr($0,1,1) =="D" && substr($0,24,1)	=="D") #total Cargos detalles
	    {
		   suma_total_cargos=suma_total_cargos+int(substr($0,26,10))
		   contador =  contador+1;
		  valor =  int(substr($0,26,10))
	    }			
	  
        if (substr($0,1,1) =="D" && substr($0,24,1) =="C")#total Abono detalle
	    {
		 suma_total_abono=suma_total_abono+int(substr($0,26,10))
		 contador =  contador+1;
		 valor =  int(substr($0,26,10))
		 #aca tengo que validar que sea numerico
		 valor_salida=`echo $valor | grep "^[1234567890]*$"`
	    }			  
	    cat=1;
	}
END{
    if (total_cabecera_cargo == suma_total_cargos )
	{
	 #printf(" Son iguales los cargos \n")
	 #printf("Total cabecera cargos:[%s] \n",total_cabecera_cargo)
	 #printf(" Total deatalle cargos:[%s] \n",suma_total_cargos)	
	 printf("%s",ok_cargo)	
	}
	else
	{
	 #printf("\n Son distintos los cargos \n")
	 #printf("Total cabecera cargos:[%s] \n",total_cabecera_cargo)
	 #printf("Total deatalle cargos:[%s] \n",suma_total_cargos)		 
	 printf("%s",nok_cargo)	
 	}	 
	if (total_cabecera_abono == suma_total_abono)
	{
	 #printf("Son iguales los abonos \n")
	 #printf("Total cabecera abonos \n:[%s]\n",total_cabecera_abono)
	 #printf("Total detalle abonos \n:[%s]\n",suma_total_abono)
	 printf("%s",ok_abono)
	}
	else
	{
	 #printf("\n Son distintos los abonos \n")
	 #printf("Total cabecera abonos:[%s] \n",total_cabecera_abono)
	 #printf("Total detalle abonos:[%s] \n",suma_total_abono)
	 printf("%s",nok_abono)
	}	 
	
	if (contador == cantidad_de_registros)
	{
	  #printf("cantidad de Registros ok \n")
	  printf("%s",ok_cantidad)
	}
	else
	{
	  #printf("\n cantidad de registros cabecera:{%s}\n",cantidad_de_registros)
	  #printf("cantidad de registros detalle:{%s}\n",contador)
	  printf("%s",nok_cantidad)
	}	 
	
}

# 6  
Old 11-20-2014
Code:
#!/bin/bash
# isnum.sh

isnum=$(echo $1 | sed -e 's/[^[:digit:]]//g')
[ $1 != "$isnum" ] && echo "Not numerical" || echo "Is numerical"

output
--------
# isnum.sh 1212
Is numerical
# isnum.sh 12re
Not numerical


Last edited by ongoto; 11-20-2014 at 09:36 PM..
# 7  
Old 11-21-2014
Quote:
Originally Posted by tricampeon81
friends that way I can validate that the value is numeric taken

instance

var = 12re

if var = numeric then a
echo "Numerical
else
echo "not mumerico"
fi
You might be interested in this thread, where exactly this has been covered.

I hope this helps.

bakunin

Moderator's Comments:
Mod Comment Because there is nothing specifically pertaining to AIX in this thread i move it to the "Unix for Dummies" section.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script regarding non numerical or empty value

Hello Team, I need bash script to check if my output is non numerical or empty. if its then my output should display default value as 0 basically, I am reading value from txt file. most of numerical value, in case there is no numerical value or its empty, then my output should be 0. ... (5 Replies)
Discussion started by: ghpradeep
5 Replies

2. UNIX for Dummies Questions & Answers

How to use grep with numerical values?

I'm new to Unix and I have been trying to fix this problem for the past week. How would I use grep to display only certain numbers for a list. For example, if I have this list: Joe senior 4/50 John junior 25/50 Mary junior 41/50 Martha sophomore 2/50 ...How do I get a file... (1 Reply)
Discussion started by: PTcharger
1 Replies

3. UNIX for Dummies Questions & Answers

List files according to the numerical value

Hi, I have a large number of files which are named as follows. VF_50, VF_100, VF_150, VF_250, VF_300, VF_350, VF_400, VF_450, VF_500. When I do an 'ls' it arranges the files in the following way VF_100, VF_150, VF_250, VF_300, VF_350, VF_400, VF_450, VF_50, VF_500. Is there a way to... (2 Replies)
Discussion started by: lost.identity
2 Replies

4. UNIX for Dummies Questions & Answers

Numerical Labeling using sed

Hi, I have been working on this problem, but could only get so far. I have a file that looks like this (cat,chimp,(((dog,cat,cow),orangutan),((horse,((cat,dog),(cow,pig))),cat,mouse,rat))); I would like after each instance of the word 'cat' to have an incrementing numerical label. I... (5 Replies)
Discussion started by: cavanac2
5 Replies

5. Shell Programming and Scripting

COMM and Numerical Sorting

Hi, Hope someone can shed some light on this... I have two lists of numbers I am comparing using COMM. If the first list is empty, and I sort both lists like so: DIFF=`comm -3 <(echo "$EMPTY" | sort -n) <(echo "$NUMBERS" | sort -n)`I get the error: comm: file 2 is not in sorted order But... (2 Replies)
Discussion started by: LostInTheWoods
2 Replies

6. Shell Programming and Scripting

variable numerical test

Hi I have a variable which should be any number between 1 and 50. It could also be any string/empty string. I have a code written below. The point is when the variable contains string. I don't want the code below to error out. Instead fall in the else bucket. && dothis_1 || dothis_2 (1 Reply)
Discussion started by: tostay2003
1 Replies

7. UNIX for Dummies Questions & Answers

Can grep do numerical comparisons?

Say for example I have a list of numbers.. 5 10 13 48 1 could I use grep to show only those numbers that are above 10? For various reasons I can only use grep... not awk or sed etc. (7 Replies)
Discussion started by: Uss_Defiant
7 Replies

8. UNIX for Dummies Questions & Answers

how to get the value of a numerical expression

Hi, All, I want to calculate a specific value of a Gaussian distribution, say the mean is a=3, variance is b=5, the indepentent variable is x=2, how could I get the y which is the Gaussian distribution value of x. Thanks, Jenny (1 Reply)
Discussion started by: Jenny.palmy
1 Replies

9. Shell Programming and Scripting

ksh numerical RegX

I need to distinguish between numerical characters in a script: echo "Enter a number." read num if (( $num = * )) ; then exit 0 fi this RegX does not work. Any suggestions? (5 Replies)
Discussion started by: prkfriryce
5 Replies

10. Shell Programming and Scripting

Numerical Decision

I'm tryning to do something like this, I have this file: spaces12tabgoodbye spaces3tabhello I want to copy to another file the lines that have the number above 10... I thought using sort -rn but I don't know how to discard the lines that have the number below 10. Any idea? Thanks (3 Replies)
Discussion started by: pmpx
3 Replies
Login or Register to Ask a Question