有人可以帮我解决这个问题吗?我正在寻找一个SED或AWK命令,我可以用它在配置文件(Linux)中找到一个唯一的字符串,上一行并在该行的末尾添加一个字符串?
例如:
配置文件:
定义主机组{
hostgroup_name http-urls;主机组的名称
别名HTTP URL;组长名称
成员domain1.com,domain2.com,domain3.com,
#MyUniqueString
}
在上面的示例中,我想使用SED或AWK查找 #MyUniqeString ,上一行以成员开头并添加“ domain4.com“在行尾。
我在下面找到了这个问题,但我需要首先搜索文本文件中的字符串,然后在上面找一行。
Bash script: Appending text at the last character of specific line of a file
有什么建议吗?
答案 0 :(得分: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
答案 1 :(得分: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
而且时间不长配置文件):
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
答案 2 :(得分:2)
这是使用反向引用的另一种sed解决方案:
sed '{N; N; s/\(.*\)\n\(#MyUniqueString\)/\1domain4.com\n\2/g}' config.file
答案 3 :(得分: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
}
答案 4 :(得分:1)
这个awk解决方案只需要记住上一行
awk '
/#MyUniqueString/ {prev = prev "domain4.com"}
NR > 1 {print prev}
{prev=$0}
END {print prev}
'