用另一个文件中指定的值替换字段

问题描述:

我有一个包含单词之间映射的文件。我必须引用该文件,并用某些文件中的映射文字替换这些文字。例如,下面的文件具有被映射像用另一个文件中指定的值替换字段

1.12.2.4    1 
1.12.2.7    12 
1.12.2.2    5 
1.12.2.4    4 
1.12.2.6    67 
1.12.2.12    5 

我将有一个具有这些关键词(1.12.2。*)许多文件的话表。我想要搜索这些关键词,并用来自此文件的相应映射替换这些词。如何在shell中执行此操作。假设一个文件包含以下行说

The Id of the customer is 1.12.2.12. He is from Grg. 
The Name of the machine is ASB 
The id is 1.12.2.4. He is from Psg. 

执行脚本后,将编号“1.12.2.12”和“1.12.2.4”应该由5和4(从主文件称为)来代替。谁能帮我吗?

既然你还没有提供任何例子,我想这是你想要什么:

输入文件

> cat temp 
1.12.2.4 1 
1.12.2.7 12 
1.12.2.2 5 
1.12.2.4 4 
1.12.2.6 67 
1.12.2.12 5 

文件被relaced

> cat temp2 
The Id of the customer is 1.12.2.12. He is from Grg. 
The Name of the machine is ASB 
The id is 1.12.2.4. He is from Psg. 

输出

> temp.pl 
The Id of the customer is 5. He is from Grg. 
The Name of the machine is ASB 
The id is 4. He is from Psg 

> 

下面是perl脚本。

#!/usr/bin/perl 

use strict; 
use warnings; 

my %hsh=(); 

open (MYFILE, 'temp'); 
open (MYFILE2, 'temp2'); 

while (<MYFILE>) { 
[email protected] = split/\s+/; 
$hsh{$arr[0]} = $arr[1]; 
} 
my $flag; 
while(<MYFILE2>) 
{ 
$flag=0; 
my $line=$_; 
foreach my $key (keys %hsh) 
{ 
    if($line=~/$key/) 
    { 
    $flag=1; 
    $line=~s/$key/$hsh{$key}/g; 
    print $line; 
    } 
} 
    if($flag!=1) 
    { 
    print $line; 
    $flag=0; 
    } 
} 
close(MYFILE); 
close(MYFILE2); 

一个使用GNU awk方式:

awk 'FNR==NR { array[$1]=$2; next } { for (i in array) gsub(i, array[i]) }1' master.txt file.txt 

结果:

The Id of the customer is 5. He is from Grg. 
The Name of the machine is ASB 
The id is 4. He is from Psg. 

要保存输出到文件:

awk 'FNR==NR { array[$1]=$2; next } { for (i in array) gsub(i, array[i]) }1' master.txt file.txt > name_of_your_output_file.txt 

说明:

FNR==NR { ... } # FNR is the current record number, NR is the record number 
        # so FNR==NR simply means: "while we process the first file listed 
        # in this case it's "master.txt" 
array[$1]=$2  # add column 1 to an array with a value of column 2 
next    # go onto the next record 

{     # this could be written as: FNR!=NR 
        # so this means "while we process the second file listed..." 
for (i in array) # means "for every element/key in the array..." 
gsub(i, array[i]) # perform a global substitution on each line replacing the key 
        # with it's value if found 
}1    # this is shorthand for 'print' 

添加单词边界使匹配更为严格:

awk 'FNR==NR { array[$1]=$2; next } { for (i in array) gsub("\\<"i"\\>", array[i]) }1' master.txt file.txt 
+1

显然,如果'master.txt'中的键过于相似,就会中断 – Steve

+0

嗨..我希望这些值被写入文件中。我怎样才能做到这一点?我是shell脚本的新手。对不起:(..在此先感谢.. – user1667630

+0

如果可能请你解释它是如何工作的 – user1667630

你可以有sed为你写的一sed脚本:

的映射:

cat <<EOF> mappings 
1.12.2.4    1 
1.12.2.7    12 
1.12.2.2    5 
1.12.2.4    4 
1.12.2.6    67 
1.12.2.12    5 
EOF 

输入文件:

cat <<EOF> infile 
The Id of the customer is 1.12.2.12. He is from Grg. 
The Name of the machine is ASB 
The id is 1.12.2.4. He is from Psg. 
EOF 

基于映射(GNU SED)的脚本:

sed -r -e 's:([^ ]*) +(.*):s/\\b\1\\b/\2/g:' mappings 

输出:

s/\b1.12.2.4\b/1/g 
s/\b1.12.2.7\b/12/g 
s/\b1.12.2.2\b/5/g 
s/\b1.12.2.4\b/4/g 
s/\b1.12.2.6\b/67/g 
s/\b1.12.2.12\b/5/g 

评估与另一sed(GNU SED):

sed -r -e 's:([^ ]*) +(.*):s/\\b\1\\b/\2/g:' mappings | sed -f - infile 

输出:

The Id of the customer is 5. He is from Grg. 
The Name of the machine is ASB 
The id is 1. He is from Psg. 

注意,映射为正则表达式,例如处理过的点(.)可以表示任何字符,并且可能需要在映射文件中或在生成sed脚本时转义。

+0

这个我s不工作..执行时出现此错误。sed:-e表达式#1,字符26:无效引用\ 2's'命令的RHS – user1667630

+0

忘记了我已将别名'sed'改为'sed -r' 。我已经为相关表达式添加了-r。 – Thor

+0

仍然出现错误.. sed:文件 - 第1行:未知命令:'。' ! Somethig是错误的,我猜.. – user1667630