Search code examples
regexbashawk

Awk regex for a string of exact length that ends in ":"


I just can't get the regex right:

awk '$6 ~ /:${14}/ {print $6}' file

I need to print out the 6th field if it's 15 characters long and ends with a ":".

Here's an example: oAFKq7XS001224:


Solution

  • You need to use --posix as:

    awk --posix '{ if ($6 ~ /^.{14}:$/) print $6}' file
    

    Command in action

    From awk manual page:

    Interval expressions are only available if either --posix or --re-interval is specified on the command line.