특정 구문 뒤의 이름 바꾸기

특정 구문 뒤의 이름 바꾸기

Bash 스크립트가 있습니다. Bash 스크립트에서 이름을 변경하고 싶습니다 master02.$machine_master

    value=master02_up
    #master02
    http://master02.$domain:8080

변경 방법은 master02나중에 만 $machine_master가능합니다 .master02http://master02

예상 출력:

    value=master02_up
    #master02
    http://$machine_master.$domain:8080

답변1

사용 표준 sed:

sed 's#http://master02.\$domain:8080#http://$machine_master.$domain:8080#' file >newfile

그러면 정확한 문자열이 바뀌고 http://master02.$domain:8080결과 http://$machine_master.$domain:8080가 새 파일에 기록됩니다.

in은 "줄 끝" 패턴으로 해석되지 않도록 이스케이프되어야 합니다 $. 대체 텍스트 는 패턴이 아니기 때문에 이스케이프할 필요가 없습니다 $domain.$

패턴과 대체 텍스트에 모두 기본 구분 기호가 포함되어 있으므로 교체 명령( ) #으로 구분 기호를 사용하고 있습니다 .seds/

이 명령은 다음과 같이 단축할 수도 있습니다.

sed 's#http://master02#http://$machine_master#' file >newfile

안전한지 여부(파일 내용과 바꾸려는 텍스트 인스턴스에 따라 다름).

시험:

$ cat file
value=master02_up
#master02
http://master02.$domain:8080

$ sed 's#http://master02.\$domain:8080#http://$machine_master.$domain:8080#' file >newfile

$ cat newfile
value=master02_up
#master02
http://$machine_master.$domain:8080

관련 정보