ggupta
ggupta

Reputation: 717

Match a length of string in column using awk

I'm trying to extract the column which matches the date pattern like: YYYY-MM-DD, but didn't understand the difference between below commands

Working command

echo 1,2,3,4,2015-12-34 | awk -F, '{if($5~/^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$/) print $1}'

Not working command

echo 1,2,3,4,2015-12-34 | awk -F, '{if($5~/^[0-9]{d}-[0-9]{2}-[0-9]{2}$/)print $1}'

Can someone explain me why it's happening, am i missing a granular thing?

Upvotes: 1

Views: 250

Answers (1)

RavinderSingh13
RavinderSingh13

Reputation: 133770

You could try following and let me know if this helps you.

echo 1,2,3,4,2015-12-34 | awk --re-interval -F, '{if($5~/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/)print $1}'

OR

echo 1,2,3,4,2015-12-34 | awk --re-interval -F, 'match($NF,/[0-9]{4}-[0-9]{2}-[0-9]{2}/){print $1}'

NOTE: My awk version is OLD so I am using --re-interval you could remove it in case you have recent awk version.

Upvotes: 1

Related Questions