Curly braces in awk reg exp

I am trying to match a fixed number of digits using curly braces in awk , but I am not getting the result.

 # This outputs nothing echo "123" | awk '/^[0-9]{3}$/ {print $1;}' # This outputs 123 echo "123" | awk '/^[0-9]+$/ {print $1;}' 

Do I need to do something to use curly braces?

+6
source share
1 answer

Mac OS X awk (BSD awk ) works with the first command:

 $ echo "123" | /usr/bin/awk '/^[0-9]{3}$/ {print $1;}' 123 $ 

GNU awk no. Adding backslashes does not help GNU awk . Using the --re-interval option does, and also uses --posix .

 $ echo "123" | /usr/gnu/bin/awk --re-interval '/^[0-9]{3}$/ {print $1;}' 123 $ echo "123" | /usr/gnu/bin/awk --posix '/^[0-9]{3}$/ {print $1;}' 123 $ 

(I'm not sure where mawk 1.3.3 from 1996 came from, but it's probably time to get an updated version of awk for your machine.)

+8
source

All Articles