我正在尝试用新的Vmax LUN替换旧的3 3par。有人能帮我做个剧本来减轻这个任务吗?
是否可以创建2个文件,一个文件具有3 3par,另一个文件具有所有新的VMAX LUNs?然后替换mulipath.conf的所有内容。
目前我正在使用:
sed -i "s/360002ac000000000000001f20001add7/60002ac000000000000001f20001accb/g" multipath.conf有人能帮我让这个命令起作用吗?
sed "s/`cat 3par.txt`/`cat vmax.txt`/g" multipath.conf发布于 2019-08-27 15:01:42
$ perl -e '
open(F1,"<",shift);
open(F2,"<",shift);
while() {
chomp; # strip trailing \n from input file.
$new=;
chomp $new;
print "s/$_/$new/g\n"
}' file1.txt file2.txt
s/360002ac000000000000001f20001add7/60002ac000000000000001f20001accb/g
s/360002ac000000000000001f20001add8/60002ac000000000000001f20001accc/g
s/360002ac000000000000001f20001add9/60002ac000000000000001f20001accd/g
s/360002ac000000000000001f20001ade0/60002ac000000000000001f20001acce/g这可能是(并被写成)一个一行,我添加了linefeed,以使它更具可读性。
如果file1.txt没有与file2.txt相同的行数,则该脚本将为一个文件中没有对应行的任何行生成伪输出。所以别那么做。
要将这些更改应用于您的multipath.conf文件,请运行上面的perl一行,并将输出重定向到一个文件(例如sedscript.sed),然后运行:
sed -f sedscript.sed multipath.conf验证生成的sed脚本是否满足您的要求,然后将输出重定向到一个新文件,或者使用sed's -i选项对multipath.conf本身进行“就地”编辑。当然,首先备份,然后将其复制到-i (甚至-i.bak)无法访问的安全位置。
示例输入文件如下:
$ cat file1.txt
360002ac000000000000001f20001add7
360002ac000000000000001f20001add8
360002ac000000000000001f20001add9
360002ac000000000000001f20001ade0
$ cat file2.txt
60002ac000000000000001f20001accb
60002ac000000000000001f20001accc
60002ac000000000000001f20001accd
60002ac000000000000001f20001acce顺便说一句,只要再做一些工作,perl一行就可以在file1和file2中读取,构造一个s/old/new/g操作数组,然后将它们应用到第三个文件中(比如multipath.conf)。
例如:
#!/usr/bin/perl
# run with three arguments:
# $1 = file containing old patterns
# $2 = file containing replacements
# $3 = file to modify
# THIS SCRIPT IS A CRUDE, MINIMALIST EXAMPLE AND CONTAINS NO ERROR
# CHECKING/HANDLING CODE AT ALL. USE AT OWN RISK.
use strict;
use File::Slurp;
# hash to store the search patterns and their replacements.
my %regex=();
open(F1,"<",shift);
open(F2,"<",shift);
while() {
chomp; # strip trailing \n from input file.
my $new=;
chomp $new;
# qr// pre-compiles the regular expression, so it doesn't have to be
# compiled on every pass through the loop.
$regex{qr/$_/} = $new;
};
close(F1);
close(F2);
my $f3=shift;
my @file3=read_file($f3);
# transform and overwrite the third file.
open(F3,">",$f3);
foreach (@file3) {
foreach my $key (keys %regex) {
s/$key/$regex{$key}/g;
}
print F3;
};
close(F3);发布于 2019-08-27 14:38:43
两个名为“旧”的文件。
$ cat old
old1
old2
old3和“新”
$ cat new
new1
new2
new3您可以为sed创建脚本文件,如下所示:
$ paste old new | while read old new; do printf "s/%s/%s/g\n" "$old" "$new"; done |tee sedfile
s/old1/new1/g
s/old2/new2/g
s/old3/new3/g并将该文件与sed一起使用:
sed -f sedfile mulipath.conf > review-this.confhttps://unix.stackexchange.com/questions/537660
复制相似问题