I am trying to assign variables obtained by awk, from a 2 columned txt file. To a command, which includes every two value as two variables in it.
For example, the file I use is;
foo.txt
10 20
33 40
65 78
my command is aiming to print ;
end=20 start=10
end=40 start=33
end=78 start=65
Basically, I want to iterate the code for every line, and for output, there will be two variables from the two columns of the input file.
I am not an awk expert (I am trying my best), what I could have done so far is this fusion;
while read -r line ; do awk '{ second_variable=$2 ; first_variable=$1 ; }' ; echo "end=$first_name start=$second_name"; done <foo.txt
but it only gives this output;
end= start=
only one time without any variable. I would appreciate any suggestion. Thank you.
awkdoesn't create/set that variable in the shell (or vide versa). awk and the shell's variables are completely separate.awk, or whatever) has its own private memory where it keeps whatever variables it may have. You'd need to do something like print the variables fromawk, capture the output in bash and assign it to bash variables. You also need to be careful not to assign bash variables in a subshell, because the subshell is a separate process from the main shell, so it'd also have its own separate variables (see BashFAQ #24). One exception is environment variables, which subprocesses get a copy of.