How to pass variable to awk [duplicate]

2019-03-01 09:49发布

Possible Duplicate:
Using awk with variables

The following command is wrong, the point is I want to use $curLineNumber in awk, how can I do it? Any solution?

curLineNumber = 3
curTime=`ls -l | awk 'NR==$curLineNumber {print $NF}'`

Thanks

标签: shell awk
1条回答
我命由我不由天
2楼-- · 2019-03-01 10:15
curTime=$(ls -l | awk -v line=$curLineNumber 'NR == line { print $NF }'

The -v option is used to specify variables initialized on the command line. I chose the name line for the awk variable.

查看更多
登录 后发表回答