merge two files having the same value in bash -


i trying merge 2 files in 1 single.

file1

2015-09-30t13:30:57+01:00 6 1 2015-09-30t13:30:58+01:00 6 1 2015-09-30t13:30:59+01:00 6 1 2015-09-30t13:31:00+01:00 6 1 2015-09-30t13:31:01+01:00 6 1 2015-09-30t13:31:02+01:00 6 1 2015-09-30t13:31:04+01:00 6 1 

file2

2015-09-30t13:16:19+01:00 4 2015-09-30t13:16:20+01:00 7 2015-09-30t13:16:21+01:00 7 2015-09-30t13:16:22+01:00 8 2015-09-30t13:16:23+01:00 8 2015-09-30t13:16:24+01:00 7 2015-09-30t13:16:25+01:00 2 2015-09-30t13:16:26+01:00 4 2015-09-30t13:16:27+01:00 1 2015-09-30t13:30:58+01:00 1 

the result trying add column 2 file2 being added file1 fourth columns time match:

2015-09-30t13:30:57+01:00 6 1 4 2015-09-30t13:16:23+01:00 8 3 1 

thank help,

al.

use cut find first column , nested while loop compare firsts columns:

#!/usr/bin/bash  printf "" > file3 while read line1;                file1_first_col=$(printf "${line1}" | cut -f1 -d' ')     printf "${line1}" >> file3     while read line2;                    file2_first_col=$(printf "${line2}"| cut -f1 -d' ')         if [[ "${file1_first_col}" == "${file2_first_col}" ]];             file2_second_col=$(printf "${line2}" | cut -f2 -d' ')             printf " ${file2_second_col}" >> file3         fi     done < file2     printf "\n" >> file3 done < file1  

then print result file called file3.

note large files may slow.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -