αғsнιη
αғsнιη

Reputation: 2761

How to prevent awk from sorting the array?

I'm trying to extract those lines from data text-file:

first
second
third
fourth
fifth
sixth

based on their lines numbers pre-saved in nums text-file:

2
6
3

and I ended up by the following solution but it gives me the lines which are sorted based on their line-numbers and not according to orders in nums file.

$ awk  'NR==FNR{lines[$0];next } FNR in lines' nums data
second
third
sixth

But what I want to achieve is below output:

second
sixth
third

So, is there any option for awk to prevent/disable it from sorting the array?

Upvotes: 1

Views: 173

Answers (1)

Wintermute
Wintermute

Reputation: 44043

Handle the files the other way around:

awk 'NR == FNR { line[NR] = $0; next } { print line[$1] }' data nums

Upvotes: 4

Related Questions