External variable in awk

后端 未结 4 1441
Happy的楠姐
Happy的楠姐 2021-01-07 22:23

I have a script, in which a script snippet is:

x=3
awk \'$2=$x{print $1}\' infile

The external variable is x,

but it p

4条回答
  •  难免孤独
    2021-01-07 23:02

    GAWK also supports the following syntax:

    awk '$2 == x {print $1}' x=3 infile
    

    An interesting usage is:

    awk '$2 == x {print $1}' x=3 infile1 x=10 infile2
    

    in this case x will be equal to 3 for infile1 and equal to 10 for infile2

提交回复
热议问题