Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pick up lines from a file based on line numbers in another file

Tags:

python

bash

awk

I have two files - one contains the addresses (line numbers) and the other one data, like this:

address file:

2
4
6
7
1
3
5

data file

1.000451451
2.000589214
3.117892278
4.479511994
5.484514874
6.784499874
7.021239396

I want to randomize the data file based on the numbers of address files so I get:

2.000589214
4.479511994
6.784499874
7.021239396
1.000451451
3.117892278
5.484514874

I want to do it either in python or in bash, but didn't yet find any solution.

like image 578
hassan Avatar asked Jun 09 '17 04:06

hassan


2 Answers

If you don't mind sed, we can use process substitution to achieve this easily:

sed -nf <(sed 's/$/p/' addr.txt) data.txt
  • -n suppresses the default printing
  • -f makes sed read commands from the process substitution <(...)
  • <(sed 's/$/p/' addr.txt) creates sed print commands based on line numbers in addr.txt

Gives the output:

2.000589214
4.479511994
6.784499874
7.021239396
1.000451451
3.117892278
5.484514874
like image 185
codeforester Avatar answered Oct 03 '22 21:10

codeforester


With awk:

awk 'NR==FNR {a[NR]=$0; next} {print a[$0]}' data.txt addr.txt
  • NR==FNR {a[NR]=$0; next} creates an associative array a with keys being the record (line) number and values being the whole record, this would be applicable only for the first file (NR==FNR), which is data.txt. next makes awk to go to the next line without processing the record any further

  • {print a[$0]} prints the value from the array with keys being the currect file's (addr.txt) line (record) number

Example:

% cat addr.txt 
2
4
6
7
1
3
5

% cat data.txt 
1.000451451
2.000589214
3.117892278
4.479511994
5.484514874
6.784499874
7.021239396

% awk 'NR==FNR {a[NR]=$0; next} {print a[$0]}' data.txt addr.txt
2.000589214
4.479511994
6.784499874
7.021239396
1.000451451
3.117892278
5.484514874
like image 23
heemayl Avatar answered Oct 03 '22 22:10

heemayl