In shell for awk, can I have variable instead of f

2019-07-20 20:33发布

For awk,

Instead of file path can I have variable?? arr=$(awk -v str=start '$2 ~ "^" str "[0-9]*" { print $2; exit; }' /filepath)

Note output was in file path before and now It is in an variable "A". Tried the following:

  arr=$(awk -v str=start '$2 ~ "^" str "[0-9]*" { print $2; exit; }' ,$a)
arr=$(awk -v VAR="a" str=start '$2 ~ "^" str "[0-9]*" { print $2; exit; }' ,$a)

Doesn't work.

Previous post for more details: Visit Using awk to find a string in a file!

Any help will appreciated.

标签: shell
1条回答
Deceive 欺骗
2楼-- · 2019-07-20 21:29

This worked, thanks anubhava

Use:

arr=$(echo "$a" | awk -v str=start '$2 ~ "^" str "[0-9]*" { print $2; exit; }')
查看更多
登录 后发表回答