Shashank Vivek
Shashank Vivek

Reputation: 17494

Passing value from a file to awk script

I am trying to pass certain columns from a file to a awk script and 1 to it.The input text file "prac.txt" contains:

Shashank 12
Ram 13
Shyam 44

the awk script "awEg" contains:

#!/bin/awk -f
{
i=$2
print "val: ",$i+1;
}

the command i am firing is:

more prac.txt |./awEg

I am getting below output:

val:  1
val:  1
val:  1

Please point out my mistakes.

Upvotes: 1

Views: 92

Answers (1)

fedorqui
fedorqui

Reputation: 289495

The awk variables are not called with dollar sign. Hence, use:

print "val: ",i+1;
              ^
              no $

All together, it yields:

$ awk -f awEg.awk prac.txt    # note no need to `more prac.txt`...
val:  13
val:  14
val:  45

Upvotes: 5

Related Questions