두 개의 파일을 입력으로 sed 사용

두 개의 파일을 입력으로 sed 사용

기존 3par LUN을 새 Vmax LUN으로 교체하려고 합니다. 작업을 단순화하기 위한 스크립트 작성을 도와줄 수 있는 사람이 있나요?

3개의 LUN이 포함된 파일과 모두 새로운 VMAX LUN이 포함된 파일 등 2개의 파일을 생성할 수 있습니까? 그런 다음 모든 내용을 교체하십시오 mulipath.conf.

현재 나는 다음을 사용하고 있습니다 :

sed -i "s/360002ac000000000000001f20001add7/60002ac000000000000001f20001accb/g" multipath.conf

누구든지 명령이 작동하도록 도와줄 수 있나요?

sed "s/`cat 3par.txt`/`cat vmax.txt`/g" multipath.conf

답변1

$ 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

이것은 한 번에 한 줄씩 작성될 수 있습니다. 더 쉽게 읽을 수 있도록 줄 바꿈을 추가했습니다.

이 스크립트는 다른 파일 에 해당 라인이 없는 한 파일의 라인에 대해 가짜 출력을 생성합니다 file1.txt. file2.txt그러니 하지 마세요.

이러한 변경 사항을 파일에 적용하려면 multipath.conf위의 perl one-liner를 실행하고 출력을 파일(예: sedscript.sed)로 리디렉션한 후 다음을 실행합니다.

sed -f sedscript.sed multipath.conf

sed생성된 스크립트가 원하는 대로 작동하는지 확인한 다음 출력을 새 파일로 리디렉션하거나 sed옵션을 사용하여 -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(<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);

답변2

"old"라는 이름의 두 파일이 주어지면...

$ 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.conf

관련 정보