4

有人可以帮我解决这个问题吗?我正在寻找可用于在配置文件 (Linux) 中查找唯一字符串的 SED 或 AWK 命令,向上一行并将字符串附加到该行的末尾?

例如:

配置文件:

定义主机组{
hostgroup_name http-urls ; 主机组别
名 HTTP URL 的名称;组
成员的长名称 domain1.com, domain2.com, domain3.com,
#MyUniqueString
}

在上面的示例中,我想使用 SED 或 AWK 来查找#MyUniqeString,上一行以成员开头并在行尾附加“domain4.com”

我在下面找到了这个问题,但我需要先在文本文件中搜索字符串,然后在上面一行。

Bash脚本:在文件特定行的最后一个字符处附加文本

有什么建议么?

4

5 回答 5

4

您可以通过以下方式有效地做到这一点ed

ed yourfile <<-'EOF'
    /#MyUniqueString/ # Find the matching line
    - # Go up a line
    a # Append text
    domain4.com
    . # Stop appending
    .-1,.j # Join the line above with the appended line
    w # Write the line
EOF
于 2012-12-19T18:52:43.983 回答
2

三种不同解决方案的比较

更小,使用sed

sed -e ':a;N;/\n#MyUniqueString/{s/\n/ domain4.com\n/};H;s/\n.*$//;p;g;s/^.*\n//;ta;' config.file

这可以运行为:

sed -e '
   :a;
    N;
    /\n#MyUniqueString/{
        s/\n/ domain4.com\n/
    };
    H;
    s/\n.*$//;
    p;
    g;
    s/^.*\n//;
    ta;
   ' config.file

简介:

time sed ':a;N;/\n#MyUniqueString/{s/\n/ domain4.com\n/};H;
                      s/\n.*$//;p;g;s/^.*\n//;ta;' config.file 
define hostgroup{
hostgroup_name http-urls ; The name of the hostgroup
alias HTTP URLs ; Long name of the group
members domain1.com, domain2.com, domain3.com, domain4.com
#MyUniqueString
}



real    0m0.010s
user    0m0.000s
sys     0m0.008s

为什么不是纯 bash

由于没有 fork,这可能非常快(如果来自bash并且不是太长的配置文件):

readarray configFile < ./config.file
for ((i=${#configFile};i--;));do
    [[ "${configFile[i]}" =~ "#MyUniqueString" ]] && break
  done
configFile[i-1]+=" domain4.com"
printf "%s\n" "${configFile[@]//$'\n'/}"

分析运行:

time {
    readarray configFile < ./config.file
    for ((i=${#configFile};i--;));do
        [[ "${configFile[i]}" =~ "#MyUniqueString" ]] && break
      done
    configFile[i-1]+=" domain4.com"
    printf "%s\n" "${configFile[@]//$'\n'/}"
}

会给:

define hostgroup{
hostgroup_name http-urls ; The name of the hostgroup
alias HTTP URLs ; Long name of the group
members domain1.com, domain2.com, domain3.com, domain4.com
#MyUniqueString
}

real    0m0.001s
user    0m0.000s
sys     0m0.000s

测试awk

time awk '{a[NR]=$0}/#MyUniqueString/{a[NR-1]=a[NR-1]" domain4.com"}END{for(i=1;i<=NR;i++)print a[i]}' config.file 
define hostgroup{
hostgroup_name http-urls ; The name of the hostgroup
alias HTTP URLs ; Long name of the group
members domain1.com, domain2.com, domain3.com, domain4.com
#MyUniqueString
}

real    0m0.009s
user    0m0.004s
sys     0m0.000s
于 2012-12-19T19:25:14.280 回答
2

这是另一个使用反向引用的 sed 解决方案:

sed '{N; N; s/\(.*\)\n\(#MyUniqueString\)/\1domain4.com\n\2/g}' config.file
于 2012-12-19T19:32:32.513 回答
1

试试这个单行:

awk '{a[NR]=$0}/#MyUniqueString/{a[NR-1]=a[NR-1]"domain4.com"}END{for(i=1;i<=NR;i++)print a[i]}' configFile

测试

kent$  cat test.txt 
define hostgroup{
hostgroup_name http-urls ; The name of the hostgroup
alias HTTP URLs ; Long name of the group
members domain1.com, domain2.com, domain3.com,
#MyUniqueString
}

kent$  awk '{a[NR]=$0}/#MyUniqueString/{a[NR-1]=a[NR-1]"domain4.com"}END{for(i=1;i<=NR;i++)print a[i]}' test.txt 
define hostgroup{
hostgroup_name http-urls ; The name of the hostgroup
alias HTTP URLs ; Long name of the group
members domain1.com, domain2.com, domain3.com,domain4.com
#MyUniqueString
}
于 2012-12-19T18:46:15.090 回答
1

这个 awk 解决方案只需要记住上一行

awk '
    /#MyUniqueString/ {prev = prev "domain4.com"} 
    NR > 1 {print prev}
    {prev=$0}
    END {print prev}
'
于 2012-12-19T20:54:05.270 回答