2012-09-13 55 views
3

我有一個包含單詞之間映射的文件。我必須引用該文件,並用某些文件中的映射文字替換這些文字。例如,下面的文件具有被映射像用另一個文件中指定的值替換字段

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(從主文件稱爲)來代替。誰能幫我嗎?

回答

0

既然你還沒有提供任何例子,我想這是你想要什麼:

輸入文件

> 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); 
5

一個使用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

5

你可以有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

相關問題