-2

I need help with comparison of two files and get the positions in third file, both files will have the same fields but the order will be unsorted in 2nd file, third file will give the line number where the data is found.

eg. file1.txt
A
B
C
D

file2.txt
B
D
A
C

outputfileposition.txt
3
1
4
2

Any help appreciated, thanks in advance

user3754136
  • 509
  • 11
  • 25

2 Answers2

1

In awk

awk 'FNR==NR{a[$0]=FNR;next}{print a[$0] > "outputfileposition.txt"}' file{2,1}.txt
123
  • 10,778
  • 2
  • 22
  • 45
0

This will do the trick :

while read line
do
   grep -n $line file2.txt | grep -o ^[0-9]* >> outputfileposition.txt
done < file1.txt
blackSmith
  • 3,054
  • 1
  • 20
  • 37