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.

3
  • 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. Commented Feb 14, 2022 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. Commented Feb 15, 2022 at 0: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). One exception is environment variables, which subprocesses get a copy of. Commented Feb 15, 2022 at 0:29

3 Answers 3

4

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
Sign up to request clarification or add additional context in comments.

2 Comments

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.
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.
0

Just in Bash:

while read -r end start; do echo "end=$end start=$start"; done <foo.txt

1 Comment

I suggest to replace end start with start end.
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.