簡體   English   中英

在Linux文件的特定行末尾添加多行

[英]Appending multiple lines to the end of a particular line in Linux file

我有一個下面的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做到這一點? 任何建議將不勝感激,在此先感謝。

看看是否足夠。

tr '\n' ' ' < File | sed -r 's/(encoding=UTF-8\?>)/&\n/g'

例:

$ cat File
<?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>

$ tr '\n' ' ' < File | sed -r 's/(encoding=UTF-8\?>)/&\n/g'
<?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 '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代替每個\\ns/\\n//g )。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM