如何用引号回显 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].

相关内容