|
Setting the FS in awk
I am writing a POSIX script where I need to set the FS equal to a variable that was set prior to executing the awk command. Is there a way to set FS equal to a variable that has already been set in the script?
Example
LINE="FIELD1~FIELD2~FIELD3~FIELD4~FIELD5"
(the separator could be different each time the LINE variable is set by reading a file, which is why I cut the 7th character each time, which would always be a separator character, to know what the separator is before getting to the awk command in the next line)
FSEP=`echo "$LINE" | cut -c 7`
'echo "$LINE" | awk 'FS="$FSEP" { print $3 }'`
However, awk doesn't recognize the $FSEP variable. Any suggestions on how I could set the FS with each iteration of the awk command in a while loop? Could I possible echo the FSEP variable out to a temp file and then read it in as the FS during the execution of the awk command?
|