Give inputs externally to awk script

Hello to all,

Please some awk expert could help me.

If I want to run an awk script as "command" give it inputs externally I do:
Script.sh

Input="$1" # "$1" is the input that will be given to the script
Output=${Input%.*}.csv

awk '{$1=$1}1' $Input | awk '{...}' > $Output

and I run the script like this:

. Script.sh inputfile

Then, the output is inputfile.csv.

That works correctly, but now I have another awk script that uses 2 inputs as follow
awk '{...}' Inputfile1 Inputfile2

What is needed in order that the script accepts the 2 inputs externally similarly as I do with my above script? I mean
run the script like this:

. Script.sh Inputfile1 Inputfile2

Thanks in advance for any help.

This should work if you are using Kshell or bash:

awk ' {do-something}' "$@" >output-file

If you have more than the two file names on the command line, then:

awk '{do-something}' $1 $2 >output-file
1 Like

Hello Agama,

Many thanks for your help.

It worsk perfect.

Last question.
The "$@" in code below is when the input is only one file?

awk ' {do-something}' "$@" > output-file

And if the inputs are more than 2 should be like this?

awk '{do-something}' $1 $2 $3 >output-file

Thanks again :slight_smile:

awk ' {do-something}' "$@" > output-file

will handle multiple inputs as well.

1 Like