簡體   English   中英

當bash腳本(awk,grep,sed)中沒有一對一的響應時,基於列連接兩個文件

[英]join two file based on column when there is no one by one corespondness in bash script (awk, grep , sed)

file1.txt

112|9305|/inst.exe    
112|9305|/lkj.exe    
112|9305|/dje.jar    
112|9305|/ind.pdf    
112|9306|/ma.exe    
112|9306|/ngg.pdf    
112|9307|/jhhh.dat    
112|9312|/ee.dat    
112|9312|/qwq.dll

file2.txt

117|9305|www.gahan.com    
117|9306|www.google.com    
117|9312|www.mihan.com    
117|9307|translate.com

預期產量

112|9305|www.gahan.com/inst.exe
112|9305|www.gahan.com/lkj.exe
112|9305|www.gahan.com/dje.jar
112|9305|www.gahan.com/ind.pdf
112|9306|www.google.com/ma.exe
112|9306|www.google.com/ngg.pdf
112|9307|translate.com/jhhh.dat
112|9312|www.mihan.com/ee.dat
112|9312|www.mihan.com/qwq.dll

我想基於第二列的值將file2.txt的第三列添加到file1.txt的第三列。 實際上,我想根據第二列加入它們,但是它們之間沒有一個再見的對應關系 如何使用awk或grep或shell腳本中的sed執行這些操作。

您可以這樣使用awk:

awk 'BEGIN{FS=OFS="|"} FNR==NR{a[$2]=$3; next} $2 in a{$3=a[$2] $3} 1' file2.txt file1.txt
112|9305|www.gahan.com/inst.exe
112|9305|www.gahan.com/lkj.exe
112|9305|www.gahan.com/dje.jar
112|9305|www.gahan.com/ind.pdf
112|9306|www.google.com/ma.exe
112|9306|www.google.com/ngg.pdf
112|9307|translate.com/jhhh.dat
112|9312|www.mihan.com/ee.dat
112|9312|www.mihan.com/qwq.dll

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM