How to use environment variables in awk

Asked 1 years ago, Updated 1 years ago, 127 views

Assume that the environment variable N contains a number.

awk "NR>N"

What should I do if I want to use the value of the variable in the awk script as shown in ?

linux shellscript zsh

2022-09-30 20:17

1 Answers

Use the awk -v option.

awk-vN="$N"'NR>N'

Another possible method is to use shell variable expansion.

awk "NR>$N"

However, there are two disadvantages to this method.

Example 2:

$CMD='; print "piyopiyo"'
$ awk "BEGIN {print$CMD}"

piyopiyo
$ awk-vCMD = "$CMD" 'BEGIN {printCMD}'
; print "piyopiyo"


2022-09-30 20:17

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.