Issue
A script will transform csv file header positions A,C,B,D to A,B,C,D and output is variable VAR_INP will be used in awk to print and create new csv file.
VAR_INP="TMP1=$1 ; TMP2=$2 ; TMP3=$3 ; TMP4=$4 ; $1=TMP1 ;$3=TMP2 ;$2=TMP3 ;$4=TMP4 ;"
I need to use this VAR_INP as input command inside awk.
echo "A,C,B,D" > input.csv
awk 'BEGIN { FS=OFS","}
{
$VAR_INP
print
}' input.csv
expected output
A,B,C,D
I am not getting desired results. its printing the input as it is.
it works only if i expand the VAR_INP and run manually
Please advise.
Solution
You could do it by letting the variable become part of the awk script, e.g.:
VAR_INP='TMP1=$1 ; TMP2=$2 ; TMP3=$3 ; TMP4=$4 ; $1=TMP1 ;$3=TMP2 ;$2=TMP3 ;$4=TMP4 ;'
awk -F, -v OFS=, '{'"$VAR_INP"'}1' input.csv
Output when input.csv
contains A,C,B,D
:
A,B,C,D
Answered By - Thor Answer Checked By - Robin (WPSolving Admin)