0

I would like to write a script to match two files. I have a file which is always change and a file acts as a database.

Input file1:

1
3
5
7
9

Database matched file1:

    A   B   C   D   E   F
1   0.27776079  0.302853938 1.52415756  2.751714059 1.363932416 2.286189771
2   0.332465    0.777918524 0.705056607 0.484138872 0.443787105 0.848742839
3   0.941768856 0.19125 0.573714912 0.5040488   0.526207725 1.554118026
4   1.717348092 0.19642752  0.315945    0.1331712   0.28427498  0.30113875
5   0.802253697 0.3768849   0.426688    0.27693 0.591697038 0.3832675
6   0.2752232   0.570078    0.3847095   0.659548575 0.327469824 0.3346875
7   0.153272    0.36594447  0.19125 0.526602427 0.44771265  0.31136
8   0.637448551 0.735756919 1.284158594 0.464060016 0.259459816 0.887975536
9   0.397221469 0.20808 0.268226    0.710250679 0.493069267 0.47672443
10  0.196928    0.492713856 0.22302 0.783853054 0.303534    1.736908487
11  0.510789888 0.14948712  0.26432 0.684485438 0.683017627 0.614033957

desired output file1:

    A   B   C   D   E   F
1   0.27776079  0.302853938 1.52415756  2.751714059 1.363932416 2.286189771
3   0.941768856 0.19125 0.573714912 0.5040488   0.526207725 1.554118026
5   0.802253697 0.3768849   0.426688    0.27693 0.591697038 0.3832675
7   0.153272    0.36594447  0.19125 0.526602427 0.44771265  0.31136
9   0.397221469 0.20808 0.268226    0.710250679 0.493069267 0.47672443

I would like to extract the matched lines from the database.

head -1 database1.txt > output1.txt
grep -wf inputfile1.txt database1.txt >> output1.txt
head -1 database1.txt > output2.txt
grep -wf inputfile2.txt database1.txt >> output2.txt
head -1 database2.txt > output3.txt
grep -wf inputfile3.txt database2.txt >> output3.txt

I try to use nano command but every time need to change the syntax.

tripleee
  • 175,061
  • 34
  • 275
  • 318
Peter Chung
  • 109
  • 2
  • 10

1 Answers1

1

You can use the join command to join the 2 files on 1st column:

$ cat file1
1
3
5
7
9
$ cat file2
A   B   C   D   E   F
1 0.27776079 0.302853938 1.52415756 2.751714059 1.363932416 2.286189771
2 0.332465 0.777918524 0.705056607 0.484138872 0.443787105 0.848742839
3 0.941768856 0.19125 0.573714912 0.5040488 0.526207725 1.554118026
4 1.717348092 0.19642752 0.315945 0.1331712 0.28427498 0.30113875
5 0.802253697 0.3768849 0.426688 0.27693 0.591697038 0.3832675
6 0.2752232 0.570078 0.3847095 0.659548575 0.327469824 0.3346875
7 0.153272 0.36594447 0.19125 0.526602427 0.44771265 0.31136
8 0.637448551 0.735756919 1.284158594 0.464060016 0.259459816 0.887975536
9 0.397221469 0.20808 0.268226 0.710250679 0.493069267 0.47672443
10 0.196928 0.492713856 0.22302 0.783853054 0.303534 1.736908487
11 0.510789888 0.14948712 0.26432 0.684485438 0.683017627 0.614033957
$ sed -n '1p' file2 && join --nocheck-order file1 <(sed -n '1!p' file2)
A   B   C   D   E   F
1 0.27776079 0.302853938 1.52415756 2.751714059 1.363932416 2.286189771
3 0.941768856 0.19125 0.573714912 0.5040488 0.526207725 1.554118026
5 0.802253697 0.3768849 0.426688 0.27693 0.591697038 0.3832675
7 0.153272 0.36594447 0.19125 0.526602427 0.44771265 0.31136
9 0.397221469 0.20808 0.268226 0.710250679 0.493069267 0.47672443
$
riteshtch
  • 8,629
  • 4
  • 25
  • 38