
파일에서 Old_String을 New_String으로 어떻게 바꾸나요?
이전_문자열:
$result = $client->updateInventory("UpdateInventory.xml");신규_문자열:
$result = $client->updateInventory("/Desktop/new/UpdateInventory.xml");
줄 번호의 Old_String = file1.txt라는 파일의 5
내가 시도한 것:
sed '5s/.*/'$result = $client->updateInventory("/Desktop/new/UpdateInventory.xml");'/' file1.txt
답변1
아래 명령을 사용하여 동일한 결과를 얻을 수 있습니다
sed "5s:.*:'$'result = '$'client->updateInventory('/Desktop/new/UpdateInventory.xml');:g" filename | sed "s:'$':$:g" | tr "'" '"'
답변2
내부의 작은따옴표를 제거하세요. 그들은 외부 명령을 종료하므로 $result
확장되고 sed
아마도 완료되지 않은 명령에 대해 불평할 것입니다 s
.
대신 명령에 다른 구분 기호를 사용하면 s
슬래시 대신 거의 모든 것을 사용할 수 있습니다. 그래서
sed '5s_.*_$result = $client->updateInventory("/Desktop/new/UpdateInventory.xml");_' file1.txt
당신이 원하는 것입니다.
답변3
c
또 다른 방법은 주소로 지정된 줄을 지우는 명령을 사용하는 것입니다.
$ seq 5 | sed '2c\
foobar'
1
foobar
3
4
5
$ # with GNU-sed, assuming \ is not first character
$ seq 5 | sed '2cfoobar'
s
대체 문자열에 특정 특수 문자가 있는 경우 및 명령 모두 c
에 문제가 있습니다. 예:
$ seq 3 | sed '2s/.*/abc&xyz/'
1
abc2xyz
3
$ seq 3 | sed '2cabc\tabc'
1
abc abc
3
이를 처리하는 강력한 방법은 r
명령을 사용하는 것입니다.
$ echo 'foo&\t\n\1///\\xyz' > f1
$ seq 3 | sed -e '2rf1' -e '2d'
1
foo&\t\n\1///\\xyz
3
답변4
awk 명령을 사용하여 결과를 얻을 수도 있습니다.
awk ‘NR==5 {gsub(".*","$result = $client->updateInventory(/Desktop/new/UpdateInventory.xml);",$0);print $0}' filename| sed 's/(/&"/g' | sed 's/)/"&/g'