Use match() in nawk to find digits in number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Use match() in nawk to find digits in number
# 1  
Old 08-16-2011
Use match() in nawk to find digits in number

Hi,

I just need to check whether number of digits in a phone number is 10 or not. If I am not wrong regex will be: [1-9][0-9]{9}

I have to use this inside nawk as this is a small portion of a big program.

Code:
nawk '
BEGIN { RS="";FS=";";
regex="[1-9][0-9]{9}";
}
{
    for (i=1;i<=NF;i++) {
      if (match($i,regex))
         {print "matched"}
      else
         {print "not matched"}
    }
}
' input

input file contains:
Code:
5134704084;51347040845

Output should be
matched
not matched

but output is:
not matched
not matched

.....
Thanks.
# 2  
Old 08-16-2011
Afaik, nawk uses \{ and \} (this is not POSIX but traditional).
And you will need to use "\\{" and "\\}" in a string.
# 3  
Old 08-16-2011
@ yazu
As I told this is just a small portion of a large program and everything else is working fine inside nawk. I am using 'for', 'if' and other loops in the same nawk and it's working. so my guess is there is a mistake either in regex declaration syntax or regex itself..

thanks.
# 4  
Old 08-16-2011
Oh... I was about using braces in regex in nawk. Try:
Code:
regex="[1-9][0-9]\\{9\\}"

# 5  
Old 08-16-2011
nope, its giving wrong results again.
# 6  
Old 08-16-2011
Well... It just means that your version of nawk doesn't support interval expressions in regex at all.
You need
Code:
"^[1-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$"

# 7  
Old 08-16-2011
Thanks yazu, you were right. it worked now.

But there should be some way to do in older version as well, if we need to match 50 digits instead of 10, its irritating.

Anyways Thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Determine if first 2 digits of string match numbers

Trying to find out how to discover if the first 2 characters of a string are "22" Not sure how. I could use if ]; then echo "yes";fi But I think that will only grab the pattern 22 and not the first 2 digits. (5 Replies)
Discussion started by: newbie2010
5 Replies

2. Shell Programming and Scripting

Help with nawk (Exact Match)

I have a file with the contents below "lets say the name of the file is abcxyz" shown at the end of this. I am using nawk to find the exact ip address and the 6 lines after the match is found using the following nawk statement /usr/bin/nawk "/111.46.14.107/,printed==6 { ++printed; print; }"... (7 Replies)
Discussion started by: knijjar
7 Replies

3. Shell Programming and Scripting

Find number of digits in a word

HI, Can you tell me how to find the number of digits in a word. $cat data.txt +123456ad 87645768 Output should be 6 8 (5 Replies)
Discussion started by: ashwin3086
5 Replies

4. Shell Programming and Scripting

find common entries and match the number with long sequence and cut that sequence in output

Hi all, I have a file like this ID 3BP5L_HUMAN Reviewed; 393 AA. AC Q7L8J4; Q96FI5; Q9BQH8; Q9C0E3; DT 05-FEB-2008, integrated into UniProtKB/Swiss-Prot. DT 05-JUL-2004, sequence version 1. DT 05-SEP-2012, entry version 71. FT COILED 59 140 ... (1 Reply)
Discussion started by: manigrover
1 Replies

5. Shell Programming and Scripting

Find filenames with three digits and add zeros to make five digits

Hello all! I've looked all over the internet and this site and have come up a loss with an easy way to make a bash script to do what I want to do. I have a file with a naming convention as follows: 2012-01-18 string of words here 123.jpg 2012-01-18 string of words here 1234.jpg 2012-01-18... (2 Replies)
Discussion started by: Buzzman25
2 Replies

6. Shell Programming and Scripting

number of digits after decimal

Hi All, I have a file of decimal numbers, cat file1.txt 1.1382666907 1.2603107334 1.6118799297 24.4995857056 494.7632588468 560.7633734425 ..... I want to see the output as only 7 digits after decimal (5 Replies)
Discussion started by: senayasma
5 Replies

7. Shell Programming and Scripting

regex to match digits not in dates

hi all, im having problems. I need to change all number 10 in a text file to word form, or in short from 10->ten. the thing is number 10 including in dates such as 10/22/1997 or 03-10-2011 should not be changed. im having some trouble because the file contains numbers like "price range from... (11 Replies)
Discussion started by: perlishell
11 Replies

8. Shell Programming and Scripting

Count number of digits in a word

Hi all Can anybody suggest me, how to get the count of digits in a word I tried WORD=abcd1234 echo $WORD | grep -oE ] | wc -l 4 It works in bash command line, but not in scripts :mad: (12 Replies)
Discussion started by: ./hari.sh
12 Replies

9. Shell Programming and Scripting

Write a shell program to find the sum of alternate digits in a given 5-digit number

Hi Can any one please post the answer for the above program.................. (4 Replies)
Discussion started by: banta
4 Replies

10. Shell Programming and Scripting

How to pattern match on digits and then increment?

I have a log file that ends in a ".xxx" where xxx are digits but I don't necessarily know what digits they are. The log file rotates automatically and is auto-incrementing - starting at .001. So the example would be: file-name.005 If the file ends in .005 and the log rotates, it logically... (2 Replies)
Discussion started by: sdutto01
2 Replies
Login or Register to Ask a Question