"교체 명령의 잘못된 플래그: '{" sed를 사용하여 한 파일에서 다른 파일로 문자열을 바꿀 때

"교체 명령의 잘못된 플래그: '{" sed를 사용하여 한 파일에서 다른 파일로 문자열을 바꿀 때

File1에 있는 문자열을 File2의 문자열로 바꾸려고 합니다.

파일 1

<IMG SRC="/Repository/GetImage.dll?baseHref=Orange/2011/03/27&amp;EntityID=Ad12911&amp;imgExtension=" />
<IMG SRC="/Repository/GetImage.dll?baseHref=Orange/2011/03/20&amp;EntityID=Ad13304&amp;imgExtension=" />
<IMG SRC="/Repository/GetImage.dll?baseHref=Orange/2010/08/29&amp;EntityID=Ad13724&amp;imgExtension=" />

파일 2

/getimage.dll?path=Orange/2011/03/27/129/Img/Ad1291103.gif
/getimage.dll?path=Orange/2011/03/20/133/Img/Ad1330402.gif
/getimage.dll?path=Orange/2010/08/29/137/Img/Ad1372408.gif

이 명령을 실행할 때

$ sed -e 's/.*SRC="\/Repository\([^"]*\)".*/\1/p{r File1' -e 'd}' File2

이 오류가 발생합니다.

sed: 1: "s/.*SRC="\/Repository\( ...": bad flag in substitute command: '{'

정규 표현식에 문제가 있나요?

내가 달성하려는 결과는 File1을 다음과 같이 만드는 것입니다.

파일 1

<IMG SRC="/Repository/getimage.dll?path=Orange/2011/03/27/129/Img/Ad1291103.gif" />
<IMG SRC="/Repository/getimage.dll?path=Orange/2011/03/20/133/Img/Ad1330402.gif" />
<IMG SRC="/Repository/getimage.dll?path=Orange/2010/08/29/137/Img/Ad1372408.gif" />

답변1

File1큰따옴표 안의 모든 내용을 그때부터 얻은 새 이미지 이름으로 바꾸 려면 File2awk를 사용합니다.

awk -F'"' 'NR==FNR{a[i++]=$1;next}{print $1 FS a[j++] FS $3}' File2 File1

출력은 다음과 같습니다.

<IMG SRC="/getimage.dll?path=Orange/2011/03/27/129/Img/Ad1291103.gif" />
<IMG SRC="/getimage.dll?path=Orange/2011/03/20/133/Img/Ad1330402.gif" />
<IMG SRC="/getimage.dll?path=Orange/2010/08/29/137/Img/Ad1372408.gif" />

답변2

당신이 거기에서 무엇을 하려는지 모르겠지만 내 sed-fu가 그다지 강력하지 않기 때문에 당신이 내가 모르는 신비한 구문을 사용하고 있는 것 같습니다. sed에 어떤 문제가 있는지 말할 수 없기 때문에(그러나 교육받은 추측에 따르면 대체 문자열에 포함된 특수 문자( /etc. ?)가 문제를 일으키는 것으로 추측됩니다) Perl 대안을 제공하겠습니다.

perl -i -pe 'BEGIN{open($f,shift); while(<$f>){chomp; push @F,$_}}
            $k=shift(@F); s/(.*SRC=.)([^"]*)/$1$k/' file2 file1 

내용을 더 명확하게 하기 위해 주석 처리된 스크립트로 작성된 동일한 내용입니다. 위 줄에서 -i실제 입력 파일이 다음과 같이 변경됩니다 sed -i.

#!/usr/bin/env perl

## This is the equivalent of the BEGIN{} block.
## @ARGV is the array of arguments and shift returns
## the first element of it. This is file2 which is
## then opened, each line is read, its trailing \n
## is removed by chomp and it is then added to the @F array.
my $file=shift(@ARGV);
open($f,$file);
while(<$f>){chomp; push @F,$_}

## This is the rest of the oneliner above. The -pe options
## cause the file to be read and each line printed after 
## the script is applied. Since the previous block removed 
## file2 from @ARGV, this is applied to file1 only.
while (<>) {
    ## Remove the 1st item of @F. This is a line of file2.
    $k=shift(@F);

    ## Make the substitution. The \ before the " is not 
    ## needed, I just added it here because otherwise, the 
    ## syntax highlighting is broken. 
    s/(.*SRC=.)([^\"]*)/$1$k/;
    ## This print is implied by the -p flag
    print;
}

답변3

이 오류는 정규식이 잘못되었다는 것이 아니라 sed 명령이 잘못되었음을 알려줍니다. 이 s명령을 다음 명령과 {구분 하려면 개행 또는 세미콜론을 사용해야 합니다 . 마찬가지로 별도의 매개변수에 넣을 수도 있습니다 -e.

sed -e /.src="/저장소([^"])".*/\1/p' -e '{' -e 'r 파일1' -e 'd' -e '}' 파일2

하지만 이렇게 하면 원하는 효과를 얻을 수 없습니다. input 에서 접두어와 다음 큰따옴표로 시작하는 부분을 제거하고 …SRC="Repository/, 대체된 줄만 인쇄하고( 명령 및 아래 p플래그 로 인해 s) d각 입력 줄의 복사본을 삽입합니다(일치 File1여부).

두 파일의 데이터를 일치시키려면 sed보다 더 강력한 도구가 필요합니다.또는모두 좋은 선택입니다.

1기술적으로 sed는 Turing 완전하지만 sed에서 그렇게 하는 것은 매우 복잡하고 모호합니다.

관련 정보