따옴표를 사용하여 ini 파일의 범주 내에서 문자열을 에코하는 방법

따옴표를 사용하여 ini 파일의 범주 내에서 문자열을 에코하는 방법

다음과 같은 ini 파일이 있습니다.

[Admin Prefixes]
# Here you can add custom prefixes to specific players or flags that are shown when using $admin_prefix$.
# Syntax: "type" "info" "prefix" "[expiration date]"

#"name" "OciXCrom" "[Timed Prefix]" "31.12.2030"
#"name" "OciXCrom" "[Scripter]"
#"steam" "STEAM_0:0:50153248" "[CM Creator]"
#"ip" "127.0.0.1" "[BOT]"
#"flag" "l" "[Head Admin]"
#"flag" "d" "[Server Admin]"
#"flag" "e" "[Test Admin]"
#"flag" "mnp" "[Premium]"
#"flag" "b" "[VIP]"
"flag" "s" "&x06]~Moderator~["
"flag" "r" "&x07]~ADMIN~["
"flag" "d" "&x06]~ADMIN~["
"flag" "f" "&x04]~ADMIN~["



[Chat Colors]
# Here you can add different chat colors to specific players or flags that are shown when using $chat_color$.
# Syntax: "type" "info" "chat color" "[expiration date]"

"flag" "s" "&x07"
"flag" "r" "&x07"
"flag" "d" "&x06"
"flag" "f" "&x01"
"flag" "" "&x01"


[Name Customization]
# Here you can modify the name shown for certain players when using $custom_name$.
# Syntax: "type" "info" "custom name" "[expiration date]"

"name" "OciXCrom" "&x03Oci&x04XC&x03rom"

"steam" "STEAM_0:0:50153248" "[CM Creator]"예를 들어 항목을 삽입하고 싶습니다.[관리자 접두사]부분

어떻게 하나요? 이 프로세스를 자동화하기 위해 bash 스크립트를 만들고 싶기 때문입니다.

저는 Linux를 처음 사용합니다. 누구든지 저를 도와주실 수 있나요? :)

답변1

Echo는 최고의 도구가 아닐 수도 있습니다. 새 줄을 삽입할 위치를 결정한 다음 sed의 추가 옵션을 사용하세요. 예를 들어 파일 끝에 추가합니다.

sed '$a "steam" "STEAM_0:0:50153248" "[CM Creator]"' your_ini_file

15행 뒤에 추가하려면 $를 15로 바꾸거나, 행 뒤에 고유한 패턴을 추가하려면 $를 /PATTERN/으로 바꾸십시오.

답변2

awk를 사용하면 대상 섹션의 비어 있지 않은 마지막 줄 뒤에 새 텍스트를 추가할 수 있습니다.

$ cat tst.awk
/^\[/ { prt() }
{ rec[++numLines] = $0 }
NF { lastPopulated = numLines }
END { prt() }

function prt(   i) {
    for ( i=1; i<=lastPopulated; i++ ) {
        print rec[i]
    }
    if ( rec[1] == tgtSect ) {
        print newText
    }
    for ( ; i<=numLines; i++ ) {
        print rec[i]
    }
    numLines = 0
}

$ awk -v tgtSect='[Admin Prefixes]' -v newText='"steam" "STEAM_0:0:50153248" "[CM Creator]"' -f tst.awk file
[Admin Prefixes]
# Here you can add custom prefixes to specific players or flags that are shown when using $admin_prefix$.
# Syntax: "type" "info" "prefix" "[expiration date]"

#"name" "OciXCrom" "[Timed Prefix]" "31.12.2030"
#"name" "OciXCrom" "[Scripter]"
#"steam" "STEAM_0:0:50153248" "[CM Creator]"
#"ip" "127.0.0.1" "[BOT]"
#"flag" "l" "[Head Admin]"
#"flag" "d" "[Server Admin]"
#"flag" "e" "[Test Admin]"
#"flag" "mnp" "[Premium]"
#"flag" "b" "[VIP]"
"flag" "s" "&x06]~Moderator~["
"flag" "r" "&x07]~ADMIN~["
"flag" "d" "&x06]~ADMIN~["
"flag" "f" "&x04]~ADMIN~["
"steam" "STEAM_0:0:50153248" "[CM Creator]"



[Chat Colors]
# Here you can add different chat colors to specific players or flags that are shown when using $chat_color$.
# Syntax: "type" "info" "chat color" "[expiration date]"

"flag" "s" "&x07"
"flag" "r" "&x07"
"flag" "d" "&x06"
"flag" "f" "&x01"
"flag" "" "&x01"


[Name Customization]
# Here you can modify the name shown for certain players when using $custom_name$.
# Syntax: "type" "info" "custom name" "[expiration date]"

"name" "OciXCrom" "&x03Oci&x04XC&x03rom"

또는 귀하의 경우와 같이 새 텍스트가 이미 입력 파일에 존재하고 주석 처리된 경우 주석 처리를 제거할 수 있습니다(존재하지 않는 경우 계속 추가됩니다).

$ cat tst.awk
/^\[/ { prt() }
{ rec[++numLines] = $0 }
NF { lastPopulated = numLines }
END { prt() }

function prt(   i,text) {
    for ( i=1; i<=lastPopulated; i++ ) {
        if ( rec[1] == tgtSect )  {
            text = rec[i]
            sub(/^[[:space:]]*#[[:space:]]*/,"",text)
            if ( text == newText ) {
                rec[i] = newText
                tgtSect = ""
            }
        }
        print rec[i]
    }
    if ( rec[1] == tgtSect ) {
        print newText
    }
    for ( ; i<=numLines; i++ ) {
        print rec[i]
    }
    numLines = 0
}

$ awk -v tgtSect='[Admin Prefixes]' -v newText='"steam" "STEAM_0:0:50153248" "[CM Creator]"' -f tst.awk file
[Admin Prefixes]
# Here you can add custom prefixes to specific players or flags that are shown when using $admin_prefix$.
# Syntax: "type" "info" "prefix" "[expiration date]"

#"name" "OciXCrom" "[Timed Prefix]" "31.12.2030"
#"name" "OciXCrom" "[Scripter]"
"steam" "STEAM_0:0:50153248" "[CM Creator]"
#"ip" "127.0.0.1" "[BOT]"
#"flag" "l" "[Head Admin]"
#"flag" "d" "[Server Admin]"
#"flag" "e" "[Test Admin]"
#"flag" "mnp" "[Premium]"
#"flag" "b" "[VIP]"
"flag" "s" "&x06]~Moderator~["
"flag" "r" "&x07]~ADMIN~["
"flag" "d" "&x06]~ADMIN~["
"flag" "f" "&x04]~ADMIN~["



[Chat Colors]
# Here you can add different chat colors to specific players or flags that are shown when using $chat_color$.
# Syntax: "type" "info" "chat color" "[expiration date]"

"flag" "s" "&x07"
"flag" "r" "&x07"
"flag" "d" "&x06"
"flag" "f" "&x01"
"flag" "" "&x01"


[Name Customization]
# Here you can modify the name shown for certain players when using $custom_name$.
# Syntax: "type" "info" "custom name" "[expiration date]"

"name" "OciXCrom" "&x03Oci&x04XC&x03rom"

"steam"...이제 블록 끝에 추가 줄이 추가되는 대신 대상 블록의 앞부분에 있는 기존 줄의 주석 처리가 해제 됩니다.

두 번째 스크립트는 또한 새 텍스트가 입력 파일에 이미 있고 주석 처리가 해제된 경우(예를 들어 이전에 이 스크립트를 실행한 경우) 새 텍스트가 다시 추가되지 않음을 의미합니다.

두 번째 스크립트는 POSIX 문자 클래스를 지원하는 awk에 의존합니다. 그렇지 않은 경우 각 스크립트 [[:space:]][ \t].

관련 정보