Hi. How can I read row number from one file and print that corresponding record present at that row in another file.
eg
file1
1
3
5
7
9
file2
11111
22222
33333
44444
55555
66666
77777
88888
99999
output should contain
11111
33333
55555
77777
99999
i want to do this on files having millions of records. This is just the scenario. i am trying using awk but unable to do.
Are the line numbers in file1 always in sorted order as they are in your sample?
yes. They are in sorted order. i want output like this
line1:11111
line3:33333
line5:55555
line7:77777
line9:99999
I am trying the following code but it is partly correct
awk 'NR==FNR{a[$0]++}FNR in a {print "line"FNR":"$0}' file1 file2
i get the following output
line1:1
line3:5
line5:9
line1:11111
line3:33333
line5:55555
line7:77777
line9:99999
You're very close. Try:
awk 'NR==FNR{a[$0]++;next}FNR in a {print "line"FNR":"$0}' file1 file2
Note that the "++" slows down the process, but doesn't change the results. You should get the same output with:
awk 'NR==FNR{a[$0];next}FNR in a{print "line"FNR":"$0}' file1 file2