awk vlookup 并打印

awk vlookup 并打印

我有一个逗号分隔的文件

# cat data

smartplayer,2222,off
smartplayer,1111,on
scorer,0000,

我想在 $1 中搜索 smartplayer,从 $3 中获取其状态并像这样打印

off-smart-player 2222
on-smart-player 1111
scorer 0000

我运行这个命令,但它的打印是这样的

# awk '{ if ($1 == smartplayer ) {print $3"-smart-player", $2}}'  data

-smart-player 
-smart-player 

答案1

您需要指定字段分隔符:

awk -F, '$1=="smartplayer"{ print $3"-smart-player", $2; next }
                          { $1=$1; print }' data

答案2

$ awk -F, '$1=="smartplayer"{$1=$3"-smart-player"} {print $1, $2}' data
off-smart-player 2222
on-smart-player 1111
scorer 0000

相关内容