-1

I have created a shell script which reads from csv File A (INPUT.csv) and then creates interim file from the data, do some operations and create another csv File B. Later at the end it merges the two files INPUT.csv and B together to generate FinalOutput.csv.

Limitation of the script is if the file name is different then I have to either rename the file or make the change in the script else it will not read. How can I make this dynamic so that whatever the name of the file is -- it should process and create the FinalOutput.csv

Sample script -

#!/bin/bash
awk -F, '{print $2}' INPUT.csv > FileB.csv
{
Operations on File B
}
paste -d "," INPUT.csv FileB.csv > FinalOutput.csv

exit 0

1 Answers1

3

Take the filenames of the input files as parameters when running the script.

You would then run the script with:

./script.sh INPUT.csv FileB.csv

Inside the script, refer to the parameters with variables "$1" and "$2", like this:

#!/bin/bash
awk -F, '{print $2}' "$1" > "$2"
{
# Operations on File B
}
paste -d "," "$1" "$2" > FinalOutput.csv

exit 0

Panki
  • 6,664