Help with NAWK regular expressions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with NAWK regular expressions
# 1  
Old 10-16-2008
Help with NAWK regular expressions

I've a file
$ cat size
1234
5678
vamsi

I want to match the lines which has just 4 digits and nothing else.
So
$ cat size | nawk ' $0 ~ /[0-9][0-9][0-9][0-9]/ {print}'
1234
5678

But when I use the repetition clause it doesn't work
cat size | nawk ' $0 ~ /[0-9]{4}/ {print}'

I actually want to filter out the lines with just 9 digits , but in a elegant manner.
Of course, /[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]/ will work, but is there any other possible way?? Thanks in advance!! Smilie
# 2  
Old 10-16-2008
Use the −−re−interval argument. The use of cat is redundant:

Code:
nawk −−re−interval '$0 ~ /[0-9]{9}/' size

Regards
# 3  
Old 10-16-2008
hmm

This is actually i am doing...

Code:
BEGIN {
errcount=0;
RC=0;
print "" > "output"
      }
{

if( $0 ~ /^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$/)
        {
        print >>"output";
        }

else if($0 ~ /^[0-9][0-9][0-9][0-9][0-9][0-9]$/)
        {
        print $0 "000" >>"output";
        }
else {
      errcount++;
     }
}

END {
if(errcount!=0)
RC=1;
exit RC;
    }

could you please tell me how to use the re interval thing here??
# 4  
Old 10-16-2008
Something like:

Code:
#!/bin/sh

nawk −−re−interval 'BEGIN {..}
}
.
.
.
}
END{...}
' size

Regards
# 5  
Old 10-16-2008
Smilie
$ nawk --re-interval '$0 ~ /[0-9]{9}/' data
nawk: unknown option --re-interval ignored


Smilie it's not working..
working on SunOS 5.9.. any clues?
# 6  
Old 10-16-2008
Quote:
Originally Posted by vamsi.coe
Smilie
$ nawk --re-interval '$0 ~ /[0-9]{9}/' data
nawk: unknown option --re-interval ignored


Smilie it's not working..
working on SunOS 5.9.. any clues?
That's because your nawk interpreter is not a GNU awk interpreter.
re-interval is a GNU AWK extension.
# 7  
Old 10-16-2008
This what worked on SunOS, to filter out the records with 9 digits:

Code:
/usr/xpg4/bin/awk  '/^[0-9]{9}/ && !/^[0-9]{10}/' file

Use only /usr/xpg4/bin/awk.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Regular Expressions

Hi Ilove unix and alwyas trying to to learn unix,but i am weak in using regular expressions.can you please give me a littel brief discription that how can i understand them and how to use .your response could lead a great hand in my unix love. (1 Reply)
Discussion started by: manoj attri
1 Replies

2. Shell Programming and Scripting

Regular expressions help

need a regex that matches when a number has a zero (0) at the end of it so like 10 20 120 30 330 1000 and so on (6 Replies)
Discussion started by: linuxkid
6 Replies

3. Shell Programming and Scripting

Regular Expressions

what elements does " /^/ " match? I did the test which indicates that it matches single lowercase character like 'a','b' etc. and '1','2' etc. But I really confused with that. Because, "/^abc/" matches strings like "abcedf" or "abcddddee". So, what does caret ^ really mean? Any response... (2 Replies)
Discussion started by: DavidHe
2 Replies

4. Shell Programming and Scripting

Regular Expressions

#!/usr/bin/perl $word = "one last challenge"; if ( $word =~ /^(\w+).*\s(\w+)$/ ) { print "$1"; print "\n"; print "$2"; } The output shows that "$1" is with result one and "$2" is with result challenge. I am confused about how this pattern match expression works step by step. I... (8 Replies)
Discussion started by: DavidHe
8 Replies

5. UNIX for Dummies Questions & Answers

Regular expressions

In regular expressions with grep(or egrep), ^ works if we want something in starting of line..but what if we write ^^^ or ^ for pattern matching??..Hope u all r familiar with regular expressions for pattern matching.. (1 Reply)
Discussion started by: aadi_uni
1 Replies

6. Shell Programming and Scripting

regular expressions

Hello, Let say I have a string with content "Free 100%". How can extract only "100" using ksh? I would this machanism to work if instead of "100" there is any kind of combination of numbers(ex. "32", "1238", "1"). I want to get only the digits. I have written something like this: ... (4 Replies)
Discussion started by: whatever
4 Replies

7. UNIX for Dummies Questions & Answers

regular expressions

Hi Gurus, I need help with regular expressions. I want to create a regular expression which will take only alpha-numeric characters for 7 characters long and will throw out an error if longer than that. i tried various combinations but couldn't get it, please help me how to get it guys. ... (2 Replies)
Discussion started by: ragha81
2 Replies

8. Shell Programming and Scripting

Help with regular expressions

I have following content in the file CancelPolicyMultiLingual3=U|PC3|EN RestaurantInfoCode1=U|restID1|1 ..... I am trying to use following matching extression \|(+) to get this PC3|EN restID1|1 Obviously it does not work. Any ideas? (13 Replies)
Discussion started by: arushunter
13 Replies

9. Shell Programming and Scripting

regular expressions

Hi, can anyone advise me how to shorten this: if || ; then I tried but it dosent seem to work, whats the correct way. Cheers (4 Replies)
Discussion started by: jack1981
4 Replies

10. Programming

regular expressions in c++

How do I use the regular expressions in c++? (2 Replies)
Discussion started by: szzz
2 Replies
Login or Register to Ask a Question