简体   繁体   中英

bash: compare between two files to append columns

I have two files. The first file is like this:

name_service1|||S_SUCCESS||1111111111
name_service1|||S_SUCCESS||1111111112
name_service1|||S_SUCCESS||1111111113
name_service1|||S_SUCCESS||1111111114
name_service1|||S_SUCCESS||1111111115
name_service1|||S_SUCCESS||1111111116
name_service1|||S_SUCCESS||1111111117
name_service1|||S_SUCCESS||1111111118

And the second file is like this:

name_service1,20160705,0900
name_service2,20160705,0800

I need do a compare between the two files by the name_service field, and when the fields are the same add the date and the hour; something like:

name_service1|20160705|0900|S_SUCCESS||1111111111
name_service1|20160705|0900|S_SUCCESS||1111111112
name_service1|20160705|0900|S_SUCCESS||1111111113
name_service1|20160705|0900|S_SUCCESS||1111111114
name_service1|20160705|0900|S_SUCCESS||1111111115
name_service1|20160705|0900|S_SUCCESS||1111111116
name_service1|20160705|0900|S_SUCCESS||1111111117
name_service1|20160705|0900|S_SUCCESS||1111111118

The first file is large, so using a while read line and doing a grep for each line of the first file takes a long time. The second file only has 5 or 6 lines.

Is there another way to do it?

Regards

Using bash , join , tr , and sed :

join -t '|' -j 1 -o 1.1,2.2,2.3,1.4,1.5,1.6,1.7 -a 1 file1 <(tr ',' '|' < file2) | \
sed 's/|$//'

The same, but without bash isms:

tr ',' '|' < file2 | \
join -t '|' -j 1 -o 1.1,2.2,2.3,1.4,1.5,1.6,1.7 -a 1 file1 - | \
sed 's/|$//'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM