Match digits in [g]awk

前端 未结 3 1722
长情又很酷
长情又很酷 2021-01-21 23:08

I\'m stumped! Trying to write an awk regex to match a string against 11 digits.

I\'ve tried:

if (var ~ /^[0-9]{11}$/ )
if (var ~ /^([0-9]){11}$/ )
if (va         


        
相关标签:
3条回答
  • 2021-01-22 00:01

    You described your problem, but didn't tell us your awk version. It is an important information.

    but this may work for your case:

    if (var ~ /^[0-9]+$/ && length(var)==11)
    

    If we know the version, there could be simpler solution.

    0 讨论(0)
  • 2021-01-22 00:10

    If you are trying to match exactly 11 consecutive digits someplace in the string:

    Using the test file:

    hi12345678910
    hi1234
    

    The windows version of awk command line:

    awk --posix "{ if ($1 ~ /[0-9]{11}/) print}" testfile.txt
    

    It printed:

    hi12345678910
    
    0 讨论(0)
  • 2021-01-22 00:12

    Doh - forgot to RTM:

                  Interval expressions are only available if either --posix or
                  --re-interval is specified on the command line.
    
    0 讨论(0)
提交回复
热议问题