Passing value as a command line argument in awk script.

I have one working awk command line. Which taking data from the �J1202523.TXT� file and generating the �brazil.dat� file. PFB code.

awk '{ DUNS = substr($0,0,9);if ( substr($0,14,3) == "089" ) print DUNS }' J1202523.TXT > Brazil.dat

But now I want to pass two parameter as a command line argument .

  1.    089 
    
  2.    brazil 
    

Please help me how can I pass command line parameter in awk script.

You can pass it with -v

awk -v outfln="brazil.dat" -v somevar="089" '{ DUNS = substr($0,0,9);if ( substr($0,14,3) == somevar ) print DUNS > outfln  }' J1202523.TXT
1 Like

i just little modify the script.

awk -v outfln="$2" -v somevar="$1" '{ DUNS = substr($0,0,9);if ( substr($0,14,3) == somevar ) print DUNS > outfln  }' J389247.TXT

it is working fine. thanks for your help. :slight_smile:

#!/bin/sh
awk -v var1="$1"  '{ ... }' >"$2".dat

Don't forget chmod +x this file.

sure :slight_smile: