Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test if the first character is a number using awk

Tags:

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
like image 460
user3437245 Avatar asked Dec 02 '22 16:12

user3437245


1 Answers

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
like image 63
sat Avatar answered Jun 13 '23 18:06

sat