Pass Parameter to an Awk Script File

Pass parameter to an awk script file

your hash bang defines the script is not shell script, it is an awk script. you cannot do it in bash way within your script.

also, what you did : echo blah|awk ... is not passing paramenter, it pipes the output of echo command to another command.

you could try these way below:

 echo "hello"|./foo.awk file -

or

var="hello"
awk -v a="$var" -f foo.awk file

with this, you have var a in your foo.awk, you could use it.

if you want to do something like shell script accept $1 $2 vars, you can write a small shellscript to wrap your awk stuff.

EDIT

No I didn't misunderstand you.

let's take the example:

let's say, your x.awk has:

{print $1}

if you do :

echo "foo" | x.awk file

it is same as:

echo "foo"| awk '{print $1}' file

here the input for awk is only file, your echo foo doesn't make sense. if you do:

  echo "foo"|awk '{print $1}' file -
or
echo "foo"|awk '{print $1}' - file

awk takes two input (arguments for awk) one is stdin one is the file, in your awk script you could:

echo "foo"|awk 'NR==FNR{print $1;next}{print $1}' - file

this will print first foo from your echo, then the column1 from file of course this example does nothing actual work, just print them all.

you can of course have more than two inputs, and don't check the NR and FNR, you could use the

ARGC   The number of elements in the ARGV array.

ARGV An array of command line arguments, excluding options and the program argument, numbered from zero to ARGC-1

for example :

echo "foo"|./x.awk file1 - file2

then your "foo" is the 2nd arg, you can get it in your x.awk by ARGV[2]

echo "foo" |x.awk file1 file2 file2 -

now it is ARGV[4] case.

I mean, your echo "foo"|.. would be stdin for awk, it could by 1st or nth "argument"/input for awk. depends on where you put the -(stdin). You have to handle it in your awk script.

How can I pass a command line argument into an awk script?

Arguments after the script are taken to be files to process. Reading them with ARGV doesn't change that.

You use the -v option to set awk variables.

awk -f script.awk -v thresh=90 input.txt

passing parameter from shell script to awk

I try to follow, but the problem persists.

awk: syntax error near line 1.
awk: bailing out near line 1.

And I was found a solution here

Solaris is well known for the fact that the some commands under /bin /usr/bin are not POSIX compliant. Instead they have additional compliant versions under /usr/xpg4 and similar hierarchies.

Thus, under Solaris you can use just:

/usr/xpg4/bin/awk -v NAME=MACHINE '$1 == NAME' /etc/hosts

Under Solaris 10 this works.

When I use the command man awk, I found that I was running SunOS 5.9.

Then I replace awk with usr/xpg4/bin/awk

It worked!

@Barmar Thank you very much for the advice about awk command line.

-- This is my code --

input_name=$1
output_name=$2
line_split=$3

/usr/xpg4/bin/awk -v "out_name=$output_name" -v "line=$line_split" 'NR%line==1{x=++i;}{print > out_name""x".txt"}' ${input_name}

passing command line arguments to awk in shell script

As far as I understood you want to pass the Input_file(file which you want to process by script) as an argument, if this is the case then following may help you in same.

cat search.sh
#!/bin/bash
variable=$1
awk 's=index($0, "CAATCTCC"){print "line=" NR, "start position=" s}' "$variable"

./search.sh 100nt_upstream_of_mTSS.fas

Passing shell variables in awk

awk variables are a different beast than shell variables.

  1. Don't put a $ sign before awk variables in the awk script.
  2. Your dates aren't quoted in the regex conditions of your initial awk script, so don't quote them when they are variables. Double quotes inside the regex aren't going to work out in your case.

Instead try:

awk -v var="$d1var" -v vari="$d2var" '/var/||/vari /{a++}a; a==4{a=0}' daily_usage_report.log

how to pass a command line argument in the awk script

You need to do ARGC-- after reading a value from ARGV array if you don't want awk to process that as a file later. You may use:

cat sql.awk

BEGIN {
FS=";"
OFS=","
quotation="'"
tableName=ARGV[2]
ARGC--
}
FNR==1 {
$1 = $1
head = $0
next
}
{
dat = ""
for(i=1; i<=NF; i++) {
val = ($i~/[[:alpha:]]$/) ? quotation $i quotation : $i
dat = (dat != "") ? dat OFS val : val
}
printf("INSERT INTO %s ( %s ) VALUES ( %s );\n", tableName, head, dat)
}
END {
printf("\n-- Generated by %s at %s\n", ENVIRON["USER"], strftime("%Y-%m-%d %T"))
}

To run this, use command:

awk -f sql.awk data.csv DATA

Alternatively, you can just use more common -v tableName=DATA to your awk command.

AWK script command line arguments and file input

In this case, you may set ARGV[1] or ARGC inside the BEGIN block:

BEGIN {
print ARGV[1]
# Empty ARGV[1] so that it is not treated as a filename
ARGV[1]=""
}
{
print $1, $3
}
END {
print "Done"
}

man 1p awk:

ARGC The number of elements in the ARGV array.

ARGV An array of command line arguments, excluding options and the program argument, numbered from zero to ARGC−1.

The arguments in ARGV can be modified or added to; ARGC can be altered. As each input file ends, awk shall treat the next non-null element of ARGV, up to the current value of ARGC−1, inclusive, as the name of the next input file. Thus, setting an element of ARGV to null means that it shall not be treated as an input file. The name '' indicates the standard input. If an argument matches the format of an assignment operand, this argument shall be treated as an assignment rather than a file argument.



Related Topics



Leave a reply



Submit