E B E B - 2 months ago 12
Linux Question

awk checking for spaces in columns

Using a fixed record length file and trying to check for spaces for a column from position 134 and length of 160 for all spaces
Here is what I have:

awk file.test |awk '{ teststr=substr($0,134,160); if (teststr ~ /^[[:space:]]*$/) {print "ALLSPACES"teststr"end"} else {print "NOTALLSPACES"teststr"end"} } '


It always return the else part eventhough there are values on the teststr

NOTALLSPACESTHIS IS A TESTend
NOTALLSPACES end
NOTALLSPACES end


What is wrong with the awk statement /regex

E B E B
Answer

The typo was causing the issue.. updated the awk statement above with the correct answer

 awk file.test |awk '{ teststr=substr($0,134,160); if (teststr ~ /^[[:space:]]*$/) {print "ALLSPACES"teststr"end"} else {print "NOTALLSPACES"teststr"end"} } '

TYPE: the *$ in after the space