0

I got a comma-separated file

# cat data

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

I want to search for smartplayer in $1, get its state from $3 and print like this

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

I run this command but it's printing like this

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

-smart-player 
-smart-player 
2
  • 1
    BTW, you don't do vlookup operation here Feb 19, 2021 at 8:03
  • 1
    The script you posted in your question would not produce the output you say it does from the input you posted. In fact it wouldn't produce any output.
    – Ed Morton
    Feb 19, 2021 at 22:26

2 Answers 2

3

You needed to specify the field separator:

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .