0

Looking for some help, I have two files, one is a large list of various names the other is coordinates. both files start each line with an 8 digit code. I want to lookup 8 digit line code from File1 and copy the line contents to all matching line codes in File2.

(File1) Only one occurence of hash / name.

136667ED ap1_01_a_ap1_01_rails_07
035B337C ap1_01_a_arrows_005
79546F82 ap1_01_a_centreline_010
0E1D31E7 prop_bush_med_02

(File2) Some have multiple hash copies like 0E1D31E7, with different coordinates.

136667ED -1294.6945,-2376.0317,21.8279
035B337C -1314.6719,-2721.7378,12.9467
79546F82 -1283.1066,-2529.9771,12.9635
0E1D31E7 1919.4160,-1814.3889,160.5210
0E1D31E7 1919.9885,-2628.2529,0.7537  
0E1D31E7 192.0235,-2603.1790,4.9978   
0E1D31E7 192.1050,4950.3540,389.4736

Below is how I would like them, The 8 Digit code / name, copied into any code line match in file 2.

136667ED -1294.6945,-2376.0317,21.8279  136667ED ap1_01_a_ap1_01_rails_07  
035B337C -1314.6719,-2721.7378,12.9467  035B337C ap1_01_a_arrows_005       
79546F82 -1283.1066,-2529.9771,12.9635  79546F82 ap1_01_a_centreline_010   
0E1D31E7 1919.4160,-1814.3889,160.5210  0E1D31E7 prop_bush_med_02          
0E1D31E7 1919.9885,-2628.2529,0.7537    0E1D31E7 prop_bush_med_02          
0E1D31E7 192.0235,-2603.1790,4.9978     0E1D31E7 prop_bush_med_02          
0E1D31E7 192.1050,4950.3540,389.4736    0E1D31E7 prop_bush_med_02          

Join lines of text with repeated beginning

This may work, I dont know how to run any of these commands. I'm using windows.

  • What does "Some have multiple hash copies" mean?  Please add example(s).  Please do not respond in comments; edit your question to make it clearer and more complete.  P.S. It's confusing that your "large list" file is actually smaller than the other one. – Scott Jun 13 '16 at 18:19
0

Based on your input, here is the output using the paste command to standard out:

$ paste File1 File2
136667ED ap1_01_a_ap1_01_rails_07   136667ED -1294.6945,-2376.0317,21.8279
035B337C ap1_01_a_arrows_005    035B337C -1314.6719,-2721.7378,12.9467
79546F82 ap1_01_a_centreline_010    79546F82 -1283.1066,-2529.9771,12.9635

Since you have large files, you may consider redirecting the output to a file by using paste File1 File2 > mergedfile.

  • I'm using windows 10, Have installed CoreUtils for Windows - GnuWin32 Could you explain more how to do paste File1 File2 Thanks – Poly Jun 13 '16 at 18:49
  • @Poly If the files are in the same directory, run command as above; if not, give paste two arguments -- the path to File1 followed by path to File2 – KM. Jun 13 '16 at 19:02
  • I'm just running through in cmd, It does look like it will take a while seems to be working. Will this update or output a new text file? – Poly Jun 13 '16 at 19:12
  • @Poly updated answer – KM. Jun 13 '16 at 19:26
  • It never matched any of the 8 digit start codes, to the output – Poly Jun 13 '16 at 19:51
0

Using join and a shell that understands process substitution:

$ join <(sort file2) <(sort file1)
035B337C -1314.6719,-2721.7378,12.9467 ap1_01_a_arrows_005
0E1D31E7 1919.4160,-1814.3889,160.5210 prop_bush_med_02
0E1D31E7 1919.9885,-2628.2529,0.7537 prop_bush_med_02
0E1D31E7 192.0235,-2603.1790,4.9978 prop_bush_med_02
0E1D31E7 192.1050,4950.3540,389.4736 prop_bush_med_02
136667ED -1294.6945,-2376.0317,21.8279 ap1_01_a_ap1_01_rails_07
79546F82 -1283.1066,-2529.9771,12.9635 ap1_01_a_centreline_010

Or, to duplicate the joining field in the middle of the output:

$ join -o0,1.2,0,2.2 <(sort file2) <(sort file1)
035B337C -1314.6719,-2721.7378,12.9467 035B337C ap1_01_a_arrows_005
0E1D31E7 1919.4160,-1814.3889,160.5210 0E1D31E7 prop_bush_med_02
0E1D31E7 1919.9885,-2628.2529,0.7537 0E1D31E7 prop_bush_med_02
0E1D31E7 192.0235,-2603.1790,4.9978 0E1D31E7 prop_bush_med_02
0E1D31E7 192.1050,4950.3540,389.4736 0E1D31E7 prop_bush_med_02
136667ED -1294.6945,-2376.0317,21.8279 136667ED ap1_01_a_ap1_01_rails_07
79546F82 -1283.1066,-2529.9771,12.9635 79546F82 ap1_01_a_centreline_010

For nicer columns:

$ join -o0,1.2,0,2.2 <(sort file2) <(sort file1) | column -t
035B337C  -1314.6719,-2721.7378,12.9467  035B337C  ap1_01_a_arrows_005
0E1D31E7  1919.4160,-1814.3889,160.5210  0E1D31E7  prop_bush_med_02
0E1D31E7  1919.9885,-2628.2529,0.7537    0E1D31E7  prop_bush_med_02
0E1D31E7  192.0235,-2603.1790,4.9978     0E1D31E7  prop_bush_med_02
0E1D31E7  192.1050,4950.3540,389.4736    0E1D31E7  prop_bush_med_02
136667ED  -1294.6945,-2376.0317,21.8279  136667ED  ap1_01_a_ap1_01_rails_07
79546F82  -1283.1066,-2529.9771,12.9635  79546F82  ap1_01_a_centreline_010

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.