我有以下一段 xml 文档,我正在尝试对其进行一些操作。到目前为止,我已经使用“sed”来查找和替换它的各个部分,现在它已经足够接近我所需要的了。到目前为止看起来像这样
<?xml version=1.0 encoding=UTF-8?>
<AIFMReportingInfo
CreationDateAndTime="2014-12-08T00:00:00"
Version="1.2"
ReportingMemberState="GB"
xsi:noNamespaceSchemaLocation=AIFMD_DATMAN_V1.2.xsd xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance>
剩下要做的就是将“CreationDateAndTime”之后的所有内容追加到第二行的末尾,因此输出应该如下所示
<?xml version=1.0 encoding=UTF-8?>
<AIFMReportingInfo CreationDateAndTime="2014-12-08T00:00:00" Version="1.2" ReportingMemberState="GB" xsi:noNamespaceSchemaLocation=AIFMD_DATMAN_V1.2.xsd xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance>
包括我在第二行附加的每行之间的空格。有没有办法使用 sed 来做到这一点?任何建议将不胜感激,提前致谢。
最佳答案
你可以做
~$ sed '1!{:a;N;$!ba s/\n//g}' myfile
<?xml version=1.0 encoding=UTF-8?>
<AIFMReportingInfo CreationDateAndTime="2014-12-08T00:00:00" Version="1.2" ReportingMemberState="GB"xsi:noNamespaceSchemaLocation=AIFMD_DATMAN_V1.2.xsd xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance>
除了第一行 (1!
),将该行附加到模式空间 (N
),如果不是文件末尾 ( $!
) 分支来标记 ba
。在文件末尾,将每个 \n
替换为空值 (s/\n//g
)。
关于linux - 将多行附加到 Linux 文件中特定行的末尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27400137/