grab specific part of the output of the --version of qrencode

Viewed 53

I try to grab just the version number of qrencode but it does not work. Any idea why?

$ qrencode --version | head -1 | awk '{print $3}'
qrencode version 3.4.4
Copyright (C) 2006-2012 Kentaro Fukuchi

I am quite confused because this sort of techniques use to work fine with most packages:

$ gzip --version | head -1 | awk '{print $2}'
1.6

Edit: thanks for the help. This is because for qrencode the output of --version is print to stderr instead of stdout. According so answer comments this is common to some extend. A bit weird to me, but fair enough.

2 Answers

Could you please try following.

qrencode --version 2>&1 | awk 'FNR==1{print $3;exit}'

You may redirect stderr to stdout:

qrencode --version 2>&1 | head -1 |  awk '{print $3}'
                   ^^^^

Output: 3.4.4

If you want to get rid of head -1 and get the same results with awk use

qrencode --version 2>&1 | awk 'NR==1{print $3}'

See cut or awk command to print first field of first row for details.

Related