AWK - compare $0 to regular expression + variable

Hi,

I have this script:

awk -v va=45 '$0~va{print}' flo2

That returns: "4526745 1234 " (this is the only line of the file "flo2".

However, I would like to get "va" to match the begining of the line, so that is "va" is different than 45 (eg. 67, 12 ...) I would not have any output. That would be something like that:

awk -v va=45 '$0~/^${va}/{print}' flo2

but that does not work because $va is not a regular expression!

Any suggestion?

Thanks

Should work :wink:

awk -v va=45 'va==substr($1,1,2)' flo2

No, that does not work that's not the right way to reference a shell var from within 'awk' - and you had it correct originally.....

awk -v va=45 '$0 ~ ("^" va){print}' flo2

Another option would be:

awk -v va="^45" '$0~va{print}' flo2