awk

Test if the first character is a number using awk


How do I check if the first character of a string is a number using awk. I have tried below but i keep getting syntax error.

awk ' { if (substr($1,1,1) == [0-9] ) print $0 }' da.txt

Solution

  • I will suggest to use @hek2mgl solution. I have pointed out some of the problems.

    Problems:

    1. You should use regex inside /..regex../.
    2. Use ~ instead of ==.

    Valid:

    awk '{ if (substr($1,1,1) ~ /^[0-9]/ ) print $0 }' file.txt