$ perl -e '
open(F1,"<",shift);
open(F2,"<",shift);
while(<F1>) {
chomp; # strip trailing \n from input file.
$new=<F2>;
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
This could be (and was written as) a one-liner, I added linefeeds to make it more readable.
if file1.txt
do not have the same number of lines as file2.txt
, this script will produce bogus output for any lines in one file that don't have a corresponding line in the other. So don't do that.
To apply these changes to your multipath.conf
file run the above perl one-liner and redirect the output to a file (e.g. sedscript.sed
) and then run:
sed -f sedscript.sed multipath.conf
Verify that the generated sed
script does what you want and then either redirect the output to a new file, or use sed
's -i
option to do an "in-place" edit on multipath.conf
itself. Make a backup first, of course and copy it somewhere safe outside the reach of -i
(or even -i.bak
).
sample input files are:
$ cat file1.txt
360002ac000000000000001f20001add7
360002ac000000000000001f20001add8
360002ac000000000000001f20001add9
360002ac000000000000001f20001ade0
$ cat file2.txt
60002ac000000000000001f20001accb
60002ac000000000000001f20001accc
60002ac000000000000001f20001accd
60002ac000000000000001f20001acce
BTW, with a little more work this perl one-liner could be made to read in file1 and file2, construct an array of s/old/new/g operations, and then apply them to a third file (such as multipath.conf)
For example:
#!/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(<F1>) {
chomp; # strip trailing \n from input file.
my $new=<F2>;
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);
3par
,LUNs
, orVmax
. Can you show us what you are trying to do. Also don't use back ticks, they are hard to use, and have been replaced with$()
e.g.sed "s/$(cat 3par.txt)/$(cat vmax.txt)/g" multipath.conf
– ctrl-alt-delor Aug 27 '19 at 14:0860002ac000000000000001f20001accb
being one of them) that you want to use as replacements, and another file with a set of strings (360002ac000000000000001f20001add7
being one of them) that you want to search for and replace in yourmultipath.conf
file. Is this correct? – fra-san Aug 27 '19 at 14:13