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 – αғsнιη Feb 19 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 at 22:26
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.