I'm currently staring at a problem in my shell script involving awk.
My goal is to add the sum of a column of a csv file into a custom command prompt (PS1). What I currently have is the following.
colToUse=$(awk -F ';' -v var="$1" 'FNR==1 {for(counter=1; counter<= NF; counter++){ if($counter == var) break;}} END{print counter}' data.csv)
check=$(awk -F ';' 'END{ print NF }' data.csv)
if [[ $colToUse -eq check+1 ]]
then
colToUse=`expr $colToUse - 1`
fi
var=$(awk -F ';' -v loc="$colToUse" '{s+=$loc} END{print s}' data.csv)
echo $var
Now this works fine; the first awk iterates over the rows to find the header which matches with $1, the second solves a problem where it overshoots the final header for some reason, and the third awk then calculates the sum of all the values in the column of the selected header.
My problem is that the value produced will be static - When inserted into PS1, it will remain the same number. What I'm looking for is a way to escape the awk, so that I can put it into the PS1, and make it recalculate the sum whenever a command is inserted into the prompt. How would I go about doing this?