-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
Artur Meinild
  • 682
  • 5
  • 15
RAHUL SONI
  • 51
  • 5
  • It would help to see the actual script. Edit your question and include it. Hint: pass the file names as parameters. – Panki Aug 07 '20 at 12:46
  • Does this answer your question? [Pass command line arguments to bash script](https://unix.stackexchange.com/questions/32290/pass-command-line-arguments-to-bash-script) – AdminBee Aug 11 '20 at 07:12

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,221
  • 2
  • 24
  • 33
Artur Meinild
  • 682
  • 5
  • 15