I am trying to utilize a combination of awk/sed to print certain fields within a given csv. I am running into a problem with getting awk to recognize my given parameters (e.g. ./printcsv.sh 3 5 - parameters being 3 and 5 and trying to print columns 3 and 5 of a csv via awk) in the code.
#! /bin/bash
read -p "What file would you like to print? " file
read -p "What directory is this file located in? " directory
cd $directory
touch readcsv.txt
for (( i = 1; i <= $#; i=i+1 ))
do
cat $file | awk -F "," -v var=${i} 'NR==2 {print $var }' "$directory"/"$file" >> readcsv.txt
done
cat readcsv.txt
rm readcsv.txt
I've thought or creating a temporary .txt file where I'd append the printed fields to, in order to cat that .txt file to be read, but I'm having trouble (probably for a lack of knowledge of sed) of creating a table in the file, as the output is just two columns right after each other (an extremely long column). Also, I don't know if the issue lies in the "for" loop that I created, or the way I'm trying to expand the variable "i", but even if I set my parameters to say 3 and 5, my hope would be that $1 would then translate to the parameter 3 (therefore column 3 in the csv), instead it still is translating to column 1 in the awk command.
Just as a frame of reference, I am very new to Bash, all self taught through online courses, which can only help so much until you get into non-course examples.