The POSIX portable file name character set consists of the upper case letters A to Z, the lower case letters a to z, the decimal digits 0 to 9, the full stop, the underscore, and the hyphen. I want to produce an error if the field is not so. I already can check that the field must start with a "/" but am having difficulty reporting errors for illegal characters within the file path given. The code I have:

{if ($6 !~ /[a-zA-Z0-9_\/.-]{0,100}$/) print NR ": ERROR The directory contains illegal characters" }

This doesn't find illegal characters and report them as it should. Input: /Bin**/home Output: Warning illegal characters found (Because of the "**")

Looking to fix my AWK command.

  • 1
    What implementation of awk is it? IIRC they don't all support the {m,n} repetition specifier. – steeldriver Oct 13 '16 at 1:55
  • I did awk --version and got awk version 20070501 – Seb Oct 13 '16 at 1:57
  • What is your OS? – John1024 Oct 13 '16 at 2:12
  • OS El Capitan on this machine – Seb Oct 13 '16 at 2:16
  • For some strange reason, you're checking whether the last 100 characters of the path are in that set, not the entire path. If the path is, say, the character $ followed by 100 a-s, it passes. – Kaz Oct 13 '16 at 16:30

how about this ?

awk '$6~/[^a-zA-Z0-9_\/.-]/{print NR ":Illegal Characters"}' filename

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.