How to call parametrs to awk script

Hi All,
I have script to handle nulls in column2 from file1.txt and redirect its output to file2.txt

I am confused with run-time command line arguments. b'cause i normal unix we use $1 $2 for two parameters. But in awk it treat $1 as column1.

So, i tried with awk -v var1 -v var2... how to pass i was confused. i failed to achieve this..

nulls.awk

awk '
BEGIN { FS=OFS="|" }
NR2 == "" { NR2="0"}
1
' file1.txt > file2.txt

I am running this script using : sh nulls.awk

Regards,
MuniSekhar

"Thanks In Advance"

$ var='hello'
$ nawk  -v v=$var 'BEGIN{ print v }'
hello
1 Like

You reference the variable with its name (you should not use $varname):

% awk -v myvar=myvalue 'BEGIN { 
  print myvar 
}'
myvalue
1 Like

agn -- nawk was not working in my unix flavour "HP-UX"...

radoulovr - myvar=myvalue should not hardcoded, it should be passed in runtime while running that script

ex: sh nulls.awk input.txt output.txt
[this is what i am expecting]

Thanks

Use awk. I used nawk because I was on solaris.

You can use a variable instead of the hard-coded value.
Could you explain what exactly you're trying to achieve?

script name: nulls.awk

awk '
BEGIN { FS=OFS="|" }
NR2 == "" { NR2="0"}
1
' <inputfilename>  > <outputfilename> 

these <inputfilename> & <outputfilename> should be passed while running the awk script in run time... input file may differs on every run.. so, usally for general scripts i use

ex: sh replace.sh

sed 's//0/g' $1 > $2

execution : sh replace.sh inputfilename outputfilename

this way i acheived.. but i failed to achieve same like

sh nulls.awk inputfilename outputfilename -- this is my desried one

What's the error message?
You can use the same technique:

sh replace.sh <infile> <outfile>

replace.sh:

awk '
BEGIN { FS=OFS="|" }
NR2 == "" { NR2="0"}
1
' "$1" > "$2"