2017-04-18 44 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