"awk '/[0-9]{4}/' file" not works, why ?

Hello, my question comes with the following code:

[river@localhost CLI010001]$ cat  fan_get.txt 
Source                                          Speed              Status              Mode
-------------------------------------------------------------------------------------------------------
PS A,Virtual Fan Group #1                       3600 RPM           Normal              Internal
PS A,Virtual Fan Group #1,Fan #1                2700 RPM           Normal
PS A,Virtual Fan Group #1,Fan #2                4300 RPM           Normal
PS A,Virtual Fan Group #1,Fan #3                3000 RPM           Normal
PS A,Virtual Fan Group #1,Fan #4                4300 RPM           Normal
PS A,Virtual Fan Group #1,Fan #5                3000 RPM           Normal
PS A,Virtual Fan Group #1,Fan #6                4700 RPM           Normal
PS A,Virtual Fan Group #1,Fan #7                2800 RPM           Normal
PS A,Virtual Fan Group #1,Fan #8                4400 RPM           Normal
PS B,Virtual Fan Group #2                       3500 RPM           Normal              Internal
PS B,Virtual Fan Group #2,Fan #1                2500 RPM           Normal
PS B,Virtual Fan Group #2,Fan #2                4000 RPM           Normal
PS B,Virtual Fan Group #2,Fan #3                2500 RPM           Normal
PS B,Virtual Fan Group #2,Fan #4                4300 RPM           Normal
PS B,Virtual Fan Group #2,Fan #5                2800 RPM           Normal
PS B,Virtual Fan Group #2,Fan #6                4500 RPM           Normal
PS B,Virtual Fan Group #2,Fan #7                3100 RPM           Normal
PS B,Virtual Fan Group #2,Fan #8                4400 RPM           Normal
-------------------------------------------------------------------------------------------------------

[river@localhost CLI010001]$ awk '/[0-9]{4}/' fan_get.txt 
[river@localhost CLI010001]$ 

Why I get no output after execute "awk '/[0-9]{4}/' fan_get.txt " ? I think many lines match it, e.g. 4400, 3100 ... ,
Thanks!

Try:

awk '/[0-9]\{4\}/' fan_get.txt 

PS: This is a long story about interval expressions support in awk. If this won't work then it means your awk doesn't support it.

Its equivalent of the below grep

 
grep "[0-9]\{4\}" file
[river@localhost CLI010001]$ awk '/[0-9]\{4\}/' fan_get.txt
[river@localhost CLI010001]$ 

sorry, it does not work. Does it means that awk does not support all the regular expression rules ?

If you are using gawk, turn on re-interval option. --re-interval. Otherwise, go through the long way

awk '/[0-9][0-9][0-9][0-9]/' file

Some AWKs' regular expression flavor may not support interval expressions (in your example, the {4} part), even though the standard has stated (for at least 15 years) that AWK's regular expression support is the extended regular expression flavor (which includes interval expression support) plus some minor escape sequence extensions.

In particular, I remember, years ago, bumping into this when testing scripts on GNU AWK. Without the --re-interval option, the feature would not be enabled.

Perhaps your AWK lacks support for it? Which implementation are you using?

Regards,
Alister

1 Like

Thanks, I should add "--posix" or "--re-interval".