简体   繁体   中英

Fuse values from first column, when the values from second column is the same in different files in awk or bash by using awk or bash

I have two files, which consists of two columns. In each file in the first column you have a MGD_number and in the second column you have SOL_number. In File1 you have MGD226-MGD450 In File2 you have MGD451-MGD675 I want to make a new file in which base on the same SOL_number from a different file you will fuse value of MGD from the one first from the second file (and put in $2) by using awk or in bash. For example MGD226 SOL17 in file1 and MGD456 SOL81 in file2 -> MGD226 MGD456 in file3 This is prohibited: MGD251 SOL17 in file1 and MGD378 SOL81 in file1 (the same file!) -> MGD251 MGD378 in file3

These are part of my two files

File1 input

MGD278 SOL51
MGD341 SOL51
MGD412 SOL78
MGD312 SOL78

File2 input

MGD489 SOL51
MGD572 SOL51
MGD613 SOL78
MGD684 SOL16

Expected output File3

MGD278 MGD489
MGD278 MGD572
MGD341 MGD489
MGD341 MGD572
MGD412 MGD613
MGD312 MGD613

When I run

join -j 2 file1 file2 | awk '{print $2, $3}' > file3

on the files file1 and file2 as above, I get

MGD278 MGD489
MGD278 MGD572
MGD341 MGD489
MGD341 MGD572
MGD412 MGD613
MGD312 MGD613

in file3 , which is what you want.

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