I have two files as follows:
file1
:
3 1
2 4
2 1
file2
:
23
9
7
45
The second field of file1
is used to specify the line of file2
that contains the number to be retrieved and printed. In the desired output, the first field of file1
is printed and then the retrieved field is printed.
Desired output file:
3 23
2 45
2 23
Here is my attempt to solve this problem:
IFS=$'\r\n' baf2=($(cat file2));echo;awk -v av="${baf2[*]}" 'BEGIN {split(av, aaf2, / /)}{print $1, aaf2[$2]}' file1;echo;echo ${baf2[*]}
However, this script cannot use the Bash array baf2
.
The solution must be efficient since file1
has billions of lines and file2
has millions of lines in the real case.