我混合了一些 xml 文件,现在有类似的东西
<Schema>
stuff
</Schema><Schema>
stuff
</Schema><Schema>
..
我需要将它们全部拆分,以便在每个文件中都有 from <Schema>
to</Schema>
使用awk的一种方法。它使用结束标签拆分寄存器,如果在它打印所有字符之前有字符:
awk -c '
BEGIN { RS = "</Schema>" }
$0 ~ /[^[:blank:]\n]/ {
printf "%s\n", $0 RS >> FILENAME "_" ++i ".xml"
}
' infile
假设infile
内容:
<Schema>
stuff
</Schema><Schema>
more stuff
</Schema><Schema>
and more stuff
</Schema>
它产生:
==> infile_1.xml <==
<Schema>
stuff
</Schema>
==> infile_2.xml <==
<Schema>
more stuff
</Schema>
==> infile_3.xml <==
<Schema>
and more stuff
</Schema>