0

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.

juylmin
  • 47
  • 7
  • 1
    Note: assigning a variable in `awk` doesn't create/set that variable in the shell (or vide versa). awk and the shell's variables are completely separate. – Gordon Davisson Feb 14 '22 at 22:34
  • I could not understand exactly, cannot I use a variable in a shell script I assigned with an embedded code of awk? Thank you. – juylmin Feb 15 '22 at 00:09
  • No, you cannot. Each process (whether it's running bash, or `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 from `awk`, 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](http://mywiki.wooledge.org/BashFAQ/024)). One exception is environment variables, which subprocesses get a *copy* of. – Gordon Davisson Feb 15 '22 at 00:29

3 Answers3

3

In bash you only need while, read and printf:

while read -r start end
do printf 'end=%d start=%d\n' "$end" "$start"
done < foo.txt
end=20 start=10
end=40 start=33
end=78 start=65

With awk, you could do:

awk '{print "end=" $2, "start=" $1}' foo.txt
end=20 start=10
end=40 start=33
end=78 start=65

With sed you'd use regular expressions:

sed -E 's/([0-9]+) ([0-9]+)/end=\2 start=\1/' foo.txt
end=20 start=10
end=40 start=33
end=78 start=65
pmf
  • 24,478
  • 2
  • 22
  • 31
  • I want to ask something about the first code; while read -r start end do printf 'end=%d start=%d\n' "$end" "$start" done < foo.txt Which part is assigning the variables exactly? If I want to to something else rather then just printing them, I need both. Thank you. – juylmin Feb 14 '22 at 22:07
  • `read -r start end` reads a line from stdin and assigns the first two fields to the variables `$start` and `$end`, respectively (order matters). In order to read in more columns, simply add more variable names. Change the `do` part to whatever you want to do with access to the variables. – pmf Feb 14 '22 at 22:09
0

Just in Bash:

while read -r end start; do echo "end=$end start=$start"; done <foo.txt
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
0

What about using xargs?

xargs -n2 sh -c 'echo end=$1 start=$2' sh < file.txt

Demo

xargs -n2 sh -c 'echo end=$1 start=$2' sh <<INPUT
10 20
33 40
65 78
INPUT

Output

end=10 start=20
end=33 start=40
end=65 start=78
Weihang Jian
  • 7,826
  • 4
  • 44
  • 55