简体   繁体   中英

How to pass arguments to awk file from bash file

I have a bash file. Which on that I want to call one awk script. which It's name is myAwkFile.awk. The problem is: on the awk file, I have to read some parameters from the current bash file. or in other words I want to pass some parameters to the awk file. How to do that?

My bash file

.
.
a = 10
.
.
#I want to pass a to the myAwkFile.awk. How ?

awk -f myAwkFile.awk myTraceFile.tr
.
.
awk -v a="$a" -f myAwkFile.awk myTraceFile.tr

There are three ways to pass arguments to awk

One: (use this if you use variable within the BEGIN block)

awk -v var="$a" 'code' filename

Two:

awk 'code' var="$a" filename

Three: (here you use the variable as the main input for awk , no file input)

awk 'code' <<< "$a"

Always double quote the "$variable"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM