Assume that you have an AWK program file with a complex procedure: example.awk. You want to assign specific columns of a variable, rather than a whole line of the current record.
#!/usr/bin/awk -f
BEGIN {...}
... {...}
...
END {
...
var1 = ... # do something
print "var1 ... " var1
var2 = ⬛⬛⬛⬛⬛⬛
print "var2 ... " var2
}
Note that var1 is independent from the file input. Run this program from a shell:
awk -f ./example.awk ./text.txt
...
var1 ... I like chocolate cake
var2 ... chocolate
As in this example, var2 has the string that is the 3rd field/column. In the UNIX shell, it might be equivalent to
var2=$(cut -d ' ' -f 3 <<< "$var1")
How would you script var2 in AWK?