Check in awk about a value of variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check in awk about a value of variable
# 1  
Old 03-30-2009
Check in awk about a value of variable

I need to check if a variable have numeric value and if yes how many digits does it have, how do i do it?
# 2  
Old 03-30-2009
Code:
#
# return number of digits if str is all digits
# otherwise return 0
#
function countdigits( str )
{
   digits = 0
   len = split( str, a ,"" )
   for ( i = 1; i <= len; i++ )
       if (( a[i] + 0 ) == a[i] )
          digits++

   if ( len == digits )
      return digits
   else
      return 0
}

# 3  
Old 03-30-2009
Quote:
Originally Posted by tal
I need to check if a variable have numeric value and if yes how many digits does it have, how do i do it?
Something like this?

Code:
awk 'int(var) == var {print length(var)}'

Regards
# 4  
Old 03-30-2009
Hammer & Screwdriver Another approach

I added the "nope" to make it easier to see the output.

Code:
> echo 159a | awk '{ink2=ink=$0;lenx=length(ink); gsub("[:alpha:]","",ink2); leny=length(ink2); if (lenx==leny){print lenx}else{print "nope"}}'
nope
> echo 159a1 | awk '{ink2=ink=$0;lenx=length(ink); gsub("[:alpha:]","",ink2); leny=length(ink2); if (lenx==leny){print lenx}else{print "nope"}}'
nope
> echo 1591 | awk '{ink2=ink=$0;lenx=length(ink); gsub("[:alpha:]","",ink2); leny=length(ink2); if (lenx==leny){print lenx}else{print "nope"}}'
4
> echo a1591 | awk '{ink2=ink=$0;lenx=length(ink); gsub("[:alpha:]","",ink2); leny=length(ink2); if (lenx==leny){print lenx}else{print "nope"}}'
nope

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How can I assign awk's variable to shell script's variable?

I have the following script, and I want to assign the output ($10 and $5) from awk to N and L: grdinfo data.grd | awk '{print $10,$5}'| read N L output from gridinfo data.grd is: data.grd 50 100 41 82 -2796 6944 0.016 0.016 3001 2461. where N and L is suppose to be 3001 and 100. I use... (8 Replies)
Discussion started by: geomarine
8 Replies

2. Shell Programming and Scripting

Check variable

Hi people, I would like to start a review of my config variable to check whether they have been changed and if not then there is only an echo. If they have been changed to my other commands are executed. I hope you can help me. with best regards JPad edit: here my code if ;... (8 Replies)
Discussion started by: JPad
8 Replies

3. Shell Programming and Scripting

Check if a variable is having value

I have a script /root/asas with following contents. #!/bin/bash ha=`cat /etc/passwd | grep sandra` if ; then echo "Sandra is in /etc/passwd" echo "variable ha is $ha" else echo "Sandra is NOT in /etc/passwd" echo "variable ha is $ha" fi What... (3 Replies)
Discussion started by: anil510
3 Replies

4. Shell Programming and Scripting

HELP with AWK one-liner. Need to employ an If condition inside AWK to check for array variable ?

Hello experts, I'm stuck with this script for three days now. Here's what i need. I need to split a large delimited (,) file into 2 files based on the value present in the last field. Samp: Something.csv bca,adc,asdf,123,12C bca,adc,asdf,123,13C def,adc,asdf,123,12A I need this split... (6 Replies)
Discussion started by: shell_boy23
6 Replies

5. Shell Programming and Scripting

Check the value of a variable

#!/bin/sh echo "Running Script to capture ORACLE Erros" # Change Directory to the actual logs path cd /home/nv8510/lognew err_var=`grep -in "ORA-" *` if then echo "THESE ARE THE ORACLE ERROR OCCURED" echo "$err_var" echo... (7 Replies)
Discussion started by: neeraj617
7 Replies

6. Shell Programming and Scripting

using awk for setting variable but change the output of this variable within awk

Hi all, Hope someone can help me out here. I have this BASH script (see below) My problem lies with the variable path. The output of the command find will give me several fields. The 9th field is the path. I want to captured that and the I want to filter this to a specific level. The... (6 Replies)
Discussion started by: Cowardly
6 Replies

7. Shell Programming and Scripting

Check if a variable is zero

I have seen many posts for this sort of problem but I just did not know how to use it for my issue. A number is assigned to a variable and I wanted to check if it is a zero or non zero. Example of my numbers are below: 000000000000000000000000000000000000000000000000... (8 Replies)
Discussion started by: soujiv
8 Replies

8. Shell Programming and Scripting

assign awk's variable to shell script's variable?

Dear All, we have a command output which looks like : Total 200 queues in 30000 Kbytes and we're going to get "200" and "30000" for further process. currently, i'm using : numA=echo $OUTPUT | awk '{print $2}' numB=echo $OUTPUT | awk '{print $5}' my question is : can I use just one... (4 Replies)
Discussion started by: tiger2000
4 Replies

9. Shell Programming and Scripting

Check if a variable has a value assigned?

Hi, I want to check if a variable has a value assigned to it or not. I can do following - cat $Var > File1 if then echo "$Var has value" else echo "$Var is null" fi But I have to check for 3 Variables and I want to wrap it up in couple of unix statements. Any... (3 Replies)
Discussion started by: sumeet
3 Replies

10. Shell Programming and Scripting

How to check if a variable contains a .

Hi I am writing a bash script and would like to check is a variable contains a . or not ex. a=102 output ok a=1.02 output not ok Many thanks, (3 Replies)
Discussion started by: gekkos
3 Replies
Login or Register to Ask a Question