I am trying to set an awk variable field to several field at once.
Right now I can only set the variables one by one.
for line in `cat file.txt`;do
var1=`echo $line | awk -F, '{print $1}'`
var2=`echo $line | awk -F, '{print $2}'`
var3=`echo $line | awk -F, '{print $3}'`
#Some complex code....
done
I think this is costly cause it parses the linux variable several times. Is there a special syntax to set the variable at once? I know that awk has a BEGIN and END block but the reason I am trying to avoid the BEGIN and END block is to avoid nested awk.
I plan to place another loop and awk code in the #Some complex code…. part.
for line in `cat file.txt`;do
var1=`echo $line | awk -F, '{print $1}'`
var2=`echo $line | awk -F, '{print $2}'`
var3=`echo $line | awk -F, '{print $3}'`
for line2 in `cat file_old.txt`;do
vara=`echo $line2 | awk -F, '{print $1}'`
varb=`echo $line2 | awk -F, '{print $2}'`
# Do comparison of $var1,var2 and $vara,$varb , then do something with either
done
done
You can use the
IFSinternal field separator to use a comma (instead of whitespace) and do the assignments in awhileloop:This will save a copy of your current
IFS, change it to a,character, and then iterate over each line in your file. The lineset -- $line;will convert each word (separated by a comma) into a numeric-variable ($1,$2, etc.). You can either use these variables directly, or assign them to other (more meaningful) variable names.Alternatively, you could use
IFSwith the answer provided by William:They are functionally identical and it just comes down to whether or not you want to explicitly set
var1=$1or have it defined in thewhile-loop’s head.