我有兩個 .tsv 檔案,第一個比另一個長得多。第二個檔案用作與第一個中存在的標記相關聯的值的索引。如何將第二個檔案中的資訊填充到第一個檔案中?我想我可以使用 grep 或 awk,但我很難理解作為一個初學者是如何看待的。任何幫助表示贊賞!
檔案 1
item_1
item_1
item_2
item_3
item_4
item_4
檔案2
item_1 12 1002839 1231232
item_2 13 1006323 1621313
item_3 11 1052636 1931233
item_4 9 1024249 1631315
想要的結果
item_1 12 1002839 1231232
item_1 12 1002839 1231232
item_2 13 1006323 1621313
item_3 11 1052636 1931233
item_4 9 1024249 1631315
item_4 9 1024249 1631315
uj5u.com熱心網友回復:
該join實用程式對指定檔案執行等式連接并將結果寫入標準輸出。
join file1.tsv file2.tsv
uj5u.com熱心網友回復:
awk基于解決方案
- 無需對檔案進行預分類
file-2在全行的基礎上進行重復資料洗掉
|
{m,n,g}awk 'NF = NR==FNR ? _* __[$ _] : __[substr($ _, !_, match($ _,
".[ \t]"))]* (__[$ _] <NF)-(OFS="\n" $-_)' FS='^$' file1 file2
item_1 12 1002839 1231232
item_1 12 1002839 1231232
item_2 13 1006323 1621313
item_3 11 1052636 1931233
item_4 9 1024249 1631315
item_4 9 1024249 1631315
它的行為有點類似于SQL SELECTstatement INNER JOINs,但不一定相同 ::
non-de-duped file1-- 它回傳by deduped-file2( )的笛卡爾積"DD2",加入$1兩者,即使在$1內部不是唯一的DD2
e.g. what "uniq -c" looked like after I randomized it,
plus altering value of last column in some of them :
2 item_1 12 1002839 1231231
2 item_1 12 1002839 1231232
1 item_2 13 1006323 1621312
1 item_2 13 1006323 1621313
1 item_3 11 1052636 1931232
1 item_3 11 1052636 1931233
2 item_4 9 1024249 1631314
2 item_4 9 1024249 1631315
uj5u.com熱心網友回復:
# print empty line if item does not exist in file2.tsv
$ awk 'NR==FNR{a[$1]=$0; next}{$0=a[$1]}1' file2.tsv file1.tsv
item_1 12 1002839 1231232
item_1 12 1002839 1231232
item_2 13 1006323 1621313
item_3 11 1052636 1931233
item_4 9 1024249 1631315
item_4 9 1024249 1631315
# print only item if item does not exist in file2.tsv
$ awk 'NR==FNR{a[$1]=$0; next}{a[$1] ? $0=a[$1]: $0}1' file2.tsv file1.tsv
item_1 12 1002839 1231232
item_1 12 1002839 1231232
item_2 13 1006323 1621313
item_3 11 1052636 1931233
item_7
item_4 9 1024249 1631315
item_5
item_4 9 1024249 1631315
# skip line if item does not exist in file2.tsv
# $ awk 'NR==FNR{a[$1]=$0; next}{if(a[$1]){$0=a[$1]}else{next}}1' file2.tsv file1.tsv
item_1 12 1002839 1231232
item_1 12 1002839 1231232
item_2 13 1006323 1621313
item_3 11 1052636 1931233
item_4 9 1024249 1631315
item_4 9 1024249 1631315
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/497156.html
上一篇:“cp”的系統呼叫被意外修改
