简体   繁体   English

使用sed在附加文本之前附加两个新行

[英]Append two new lines before additional text using sed

I've hit a bit of a stumper (for me). (对我来说,我有点磕磕绊绊)。 I'm attempting to insert two newline characters into the RHEL5 /etc/sysconfig/iptables file during our server build process (using kickstart post-installation scripts). 我在服务器构建过程中尝试在RHEL5 / etc / sysconfig / iptables文件中插入两个换行符(使用kickstart安装后脚本)。

The specific sed command is: 具体的sed命令是:

${SED} -i "/-i lo/ a\
\n\n#Trusted Traffic\n-A INPUT -s 10.153.156.0/25,10.153.174.160/27 -d ${MGTIP} -m state --state NEW -j ACCEPT\n\n#Remote Access\n-A INPUT -s 10.120.80.0/21,10.152.80.0/21,10.153.193.0/24,172.18.1.0/24,${MGTNET}/${NUMBITS} -d ${MGTIP} -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT\n\n#Backups\n-A INPUT -s 10.153.147.192/26 -d ${BKPIP} -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT\n\n" ${IPTABLES}

This is actually part of a larger script. 这实际上是更大脚本的一部分。 ${SED}and ${IPTABLES} are already set to the necessary values. $ {SED}和$ {IPTABLES}已设置为必要值。

All of the newlines work with the exception of the first two. 除前两个外,所有新行都有效。 Or, more accurately, the second of the first two. 或者,更准确地说,前两个中的第二个。 Even the last two newlines after ACCEPT work. 即使是ACCEPT工作后的最后两个新行。 What happens with the first two newlines is that the first works, creating a newline after matching the iptables entry which contains -i lo . 前两个新行发生的情况是第一个工作,在匹配包含-i lo的iptables条目后创建换行符。 The second, however, simply inserts a literal 'n' prior to the #Trusted Traffic text. 但是,第二个只是在#Trusted Traffic文本之前插入一个文字'n'。

It ends up looking like 它最终看起来像

(snip)
-A RH-Firewall-1-INPUT -i lo -j ACCEPT
n#Trusted Traffic
-A INPUT (snip)

I've tried various methods of ensuring the second newline is inserted. 我已经尝试了各种方法来确保插入第二个换行符。 I've used two blank lines instead of \\n\\n . 我使用了两个空行而不是\\n\\n I've used two newline characters on separate lines, I've used \\\\n\\\\n . 我在单独的行上使用了两个换行符,我使用了\\\\n\\\\n Everything I've tried so far results in the same outcome: A literal 'n' being inserted instead of a second newline. 到目前为止我尝试的所有内容都会产生相同的结果:插入文字“n”而不是第二个换行符。

Does sed simply not work with two newline characters at the beginning of appended text? sed根本不能在附加文本的开头使用两个换行符吗? Is there a way to make this work that I'm simply ignorant of? 有没有办法让这项工作完全无知?

Interesting, I would have thought that one of your attempted solutions would work, but I am seeing the same behavior. 有意思的是,我原以为你尝试过的解决方案之一会起作用,但我看到了同样的行为。 Here is one potential solution: 这是一个潜在的解决方案:

${SED} -i -e "s/-i lo.*/\0\n\n/" -e "// a\
#Trusted Traffic\n-A INPUT -s 10.153.156.0/25,10.153.174.160/27 -d ${MGTIP} -m state --state NEW -j ACCEPT\n\n#Remote Access\n-A INPUT -s 10.120.80.0/21,10.152.80.0/21,10.153.193.0/24,172.18.1.0/24,${MGTNET}/${NUMBITS} -d ${MGTIP} -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT\n\n#Backups\n-A INPUT -s 10.153.147.192/26 -d ${BKPIP} -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT\n\n" ${IPTABLES}

This works by first appending the two newlines to the end of the previous line, and then doing the append. 首先将两个换行符附加到上一行的末尾,然后执行追加操作。

我不明白为什么它也不起作用,但你也可以使用替换选项而不是追加:

${SED} -i "s%-i lo.*%&\n\n#Trusted Traffic\n-A INPUT -s 10.153.156.0/25,10.153.174.160/27 -d ${MGTIP} -m state --state NEW -j ACCEPT\n\n#Remote Access\n-A INPUT -s 10.120.80.0/21,10.152.80.0/21,10.153.193.0/24,172.18.1.0/24,${MGTNET}/${NUMBITS} -d ${MGTIP} -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT\n\n#Backups\n-A INPUT -s 10.153.147.192/26 -d ${BKPIP} -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT\n\n%" ${IPTABLES}

Not sure about portability, but try: 不确定可移植性,但尝试:

${SED} '/-i lo/ a\
\
\
'"#Trusted Traffic\\
-A INPUT -s 10.153.156...
"

This technique works on BSD sed. 这种技术适用于BSD sed。 You can maintain double quotes throughout with: 您可以在整个过程中保持双引号:

${SED} "/-i lo/ a\\
\\
\\
#Trusted Traffic\\
-A INPUT -s 10.153.156...
"

In either case, there must be no whitespace between the backslash and the end of the line. 在任何一种情况下,反斜杠和行尾都必须没有空格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM