2017-04-18 43 views
0

我怎麼能以更換從filetobeprocessed.txt文本中使用SED,使用替換文件中找到的文字,多次SED

其中有名稱,電話號碼也許在它:

Name3 john.D 
Name6 mary.D 
Name7 kelly.O 
etc 

其中每行有文本。要找到的文本存在於fileA.txt中,替換字符串位於fileB.txt中。

例如,fileA.txt

可能有

NAME3 john.D,

和fileB.txt 可能

人:約翰Diamen,

所以filetobeprocessed.txt應成爲:

Person: John Diamen 
Name6 mary.D 
Name7 kelly.O 

當然,由於要處理的文件fileA和fileB很大,我們可能在搜索字符串中找到任何字符,並且在替換字符串中,因此我的示例是基本的,並且不涵蓋所有字符存在於filetobeprocessed.txt 所以我需要一種方法來使用sed來做一個搜索和替換,但是這樣做,對於fileA.txt中的每一行,以及它的等價字符串,在同一個數字行中找到,在fileB.txt

一些與SED -f -

sed -i 's/old/new/g' 
合併

SED的/ \串被替換\ B /替換字符串/ G'file.xml

什麼我不能找到,就是用此對的fileA的每一行的方法。 TXT,其中有要搜索的所有字符串,併爲每個相應的替代路線,fileB.txt

+0

請發佈所有3 fil的工作示例es與該數據的預期輸出一起提到。 –

+0

沒有添加一個非常具體的例子的原因是,我想,一個非常普遍的答案,這與查找fileA.txt的第x行中找到的每個字符串的問題有關,並將其替換爲找到的字符串在文件B的第x行。txt(完全匹配) – nutame

+0

你如何匹配fileA和fileB條目,是否有一個關鍵連接它們或是它的位置(第一到第一,第二到第二等) – karakfa

回答

0

發現在awk中:

$ awk ' 
NR==FNR {    # hash the first file to a 
    a[$0]=$0; next } 
{ 
    if(FNR in b) {  # process the third file to b 
     b[b[FNR]]=$0 
     delete b[FNR] } 
    else b[FNR]=$0  # process the second file to b 
} 
END {     # after all files are processed and in memory 
    for(i in a)   # go thru all entries of first file 
     if(i in b)  # if entry found in second file 
      print b[i] # replace with the value of third file 
     else 
      print a[i] # otherwise print the entry from the first file 
}' filetobeprocessed.txt fileA.txt fileB.txt # mind the order 
Name6 mary.D 
Person: John Diamen 
Name7 kelly.O 
+0

我不明白我可以如何使用上述命令例如,foo bar baz是什麼?一個例子與我的文件名,可能會更好,我不喜歡 – nutame

+0

@nutame工作正在進行中。完成。 –

+0

使用它,但它也應該從要處理的文件中引出未被替換的所有行,並保持原樣。現在的結果文件,大小爲5mb,而原始文件爲19mb ... – nutame

0

要求不明確的,但像這樣的東西可能會奏效。

示例文件

==> file <== 
Name1 2377 
Name2 2910 
Name3 8458 
Name4 1522 
Name5 5855 
Name6 1934 
Name7 8106 
Name8 1735 
Name9 4849 
Name10 1518 

==> fileA <== 
Name3 
Name7 

==> fileB <== 
Person: John Smith 
Person: Mr Brown 



$ awk -F'\t' 'NR==FNR {a[$1]=$2; next} 
       $1 in a {$1=a[$1]}1' <(paste fileA fileB) FS='[[:space:]]' file 

Name1 2377 
Name2 2910 
Person: John Smith 8458 
Name4 1522 
Name5 5855 
Name6 1934 
Person: Mr Brown 8106 
Name8 1735 
Name9 4849 
Name10 1518 

這個假設查找文件fileAfileB都不會太大,在這裏,因爲沒有限制,在被替換file

這也可以用sed來完成,假設替換文字中沒有特殊字符

$ sed -f <(paste fileA fileB | sed -r 's_(.*)\t(.*)_s/\1/\2/_') file