Reputation: 2115
Let's say I have two files with the same values in 1st column (different order)
The first one:
2 A
6 B
3 C
1 D
4 E
And the second one:
6 F
3 G
2 H
4 I
1 J
I'd like to sort the second file according to the 1st column of the first file. Desirable output:
2 H
6 F
3 G
1 J
4 I
Upvotes: 1
Views: 452
Reputation: 2448
Put letters from the second file to the indexes of secondArray according to the numbers: for e.g.
secondArray [6] = F, secondArray [3] = G, secondArray[2] = H
and write them, or use the order from the first array to get value by index secondArray
result[1] = secondArray[firstArray[1]]
where firstArray[1] == 2, firstArray[1] == 6 and
secondArray[firstArray[1]] == H , secondArray[firstArray[2]] ==F
Upvotes: 0
Reputation: 195249
if
try this:
awk 'NR==FNR{a[$1]=$0;next}{print a[$1]}' f2 f1
Upvotes: 3