在这里,虚荣是一个变量,我需要使用sed命令从第一个出现的“ members”单词开始,将该变量的内容放置在行的末尾
#!/bin/bash
vanity=$()
testing.txt文件包含以下信息:
define hostgroup{
hostgroup_name NA-servers ; The name of the hostgroup
alias NA region ; Long name of the group
members abc.com,def.com,ghi.com,jkl.com
}
define hostgroup{
hostgroup_name SA-servers ; The name of the hostgroup
alias SA region ; Long name of the group
members abcd.com,defg.com,ghij.com,jklm.com
}
我已经尝试过此命令:
sed "1,/members/s/$/,$vanity/" testing.txt
但它返回以下输出:
define hostgroup{,mno.com
hostgroup_name NA-servers ; The name of the hostgroup,mno.com
alias NA region ; Long name of the group,mno.com
members abc.com,def.com,ghi.com,jkl.com,mno.com
}
必需的输出:
define hostgroup{
hostgroup_name NA-servers ; The name of the hostgroup
alias NA region ; Long name of the group
members abc.com,def.com,ghi.com,jkl.com**,mno.com**
}
define hostgroup{
hostgroup_name SA-servers ; The name of the hostgroup
alias SA region ; Long name of the group
members abcd.com,defg.com,ghij.com,jklm.com
}
答案 0 :(得分:0)
使用GNU sed:
sed '/NA-servers/,/}/{s/members.*/&,mno.com/}' file
输出:
define hostgroup{ hostgroup_name NA-servers ; The name of the hostgroup alias NA region ; Long name of the group members abc.com,def.com,ghi.com,jkl.com,mno.com } define hostgroup{ hostgroup_name SA-servers ; The name of the hostgroup alias SA region ; Long name of the group members abcd.com,defg.com,ghij.com,jklm.com }
答案 1 :(得分:0)
使用GNU sed的另一种方法是仅将值附加到第一次出现的情况:
sed '0,/members.*/{s//&'"$vanity"'/;}' file
如果您尚未安装GNU sed,请尝试使用此POSIX兼容的计算机:
sed '1,/members.*/{s/members.*/&'"$vanity"'/;}' file