alex
alex

Reputation: 1439

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
.
.

Upvotes: 0

Views: 116

Answers (2)

Jotne
Jotne

Reputation: 41456

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"

Upvotes: 2

Ed Morton
Ed Morton

Reputation: 203502

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

Upvotes: 3

Related Questions