比使用 sed 更簡潔的包裝檔方法

比使用 sed 更簡潔的包裝檔方法

介紹

腳本:

sed -i '1i <?xml version=\"1.0\" encoding=\"utf-8\"?>' $1
sed -i '/<?xml version=\"1.0\" encoding=\"utf-8\"?>/a<hello>\n\t<world>' $1
sed -i "\$a\\\t<\/hello>\n<\/world>" $1

輸入:

<city id="city01">
  <name>utrecht</author>
  <population>328.577</population>
  <districts>10</districts>
  <country>netherlands</country>
</city>

輸出:

<?xml version="1.0" encoding="utf-8"?>
<hello>
  <world>
    <city id="city01">
      <name>utrecht</author>
      <population>328.577</population>
      <districts>10</districts>
      <country>netherlands</country>
    </city>
  </hello>
</world>

問題

有哪些比使用 sed 更簡潔的包裝檔方法?

答案1

正如我在評論中所說,我不明白你的實際問題是什麼。以下是執行sed腳本功能的一些更簡潔的方法:

$ printf "%s\n%s\n\t%s\n%s\n%s\n%s\n" '<?xml version="1.0" encoding="utf-8"?>' \
'<hello>' '<world>' "$(cat file)" "</world>" "</hello>"
<?xml version="1.0" encoding="utf-8"?>
<hello>
    <world>
<city id="city01">
  <name>utrecht</author>
  <population>328.577</population>
  <districts>10</districts>
  <country>netherlands</country>
</city>
</world>
</hello>

或者

$ echo -e '<?xml version="1.0" encoding="utf-8"?>' "\n<hello>\n<world>" "$(cat file)" \
"</world>\n</hello>"
<?xml version="1.0" encoding="utf-8"?> 
<hello>
<world> <city id="city01">
  <name>utrecht</author>
  <population>328.577</population>
  <districts>10</districts>
  <country>netherlands</country>
</city> </world>
</hello>

或者

$ perl -lpe 'BEGIN{
            print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<hello>\n\t<world>"
            } 
            $_="\t\t$_"; END{print "\t </world>\n</hello>"}' file
<?xml version="1.0" encoding="utf-8"?>
<hello>
    <world>
        <city id="city01">
          <name>utrecht</author>
          <population>328.577</population>
          <districts>10</districts>
          <country>netherlands</country>
        </city>
     </world>
</hello>

您可以使用 就地編輯該檔案perl -i -ple

或者

$ awk 'BEGIN{printf "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<hello>\n\t<world>";} 
        {print "\t\t",$0}END{printf "\t </world>\n</hello>\n"}' file
<?xml version="1.0" encoding="utf-8"?>
<hello>
    <world>      <city id="city01">
           <name>utrecht</author>
           <population>328.577</population>
           <districts>10</districts>
           <country>netherlands</country>
         </city>
     </world>
</hello>

或混合:

$ echo -e '<?xml version="1.0" encoding="utf-8"?>\n<hello>\n\t<world>'; 
  perl -pe '$_="\t\t$_"' file; echo -e "</world>\n</hello>"
<?xml version="1.0" encoding="utf-8"?>
<hello>
    <world>
        <city id="city01">
          <name>utrecht</author>
          <population>328.577</population>
          <districts>10</districts>
          <country>netherlands</country>
        </city>
</world>
</hello>

答案2

沒有更簡潔,但可能更易讀:

tmp=$(mktemp)
cat <<END >$tmp && mv $tmp "$1"
<?xml version="1.0" encoding="utf-8"?>
<hello>
    <world>
        $(sed 's/^/        /' "$1")
    </world>
</hello>
END

答案3

awk -v indentchar=$'\t' \
'BEGIN { print "<?xml version=\"1.0\" encoding=\"utf-8\"?>"; print "<hello>"; 
print indentchar "<world>";};
{ print indentchar indentchar $0; };
END { print indentchar "</world>"; print "</hello>"; }' file

答案4

{
  rm -f -- "$1" &&
  {
    printf '<?xml version="1.0" encoding="utf-8"?>\n<hello>\n\t<world>\n'
    paste /dev/null -
    printf '\t</hello>\n</world>\n'
  } > "$1"
} < "$1"

如果不是更短的話,也會更便攜、更有效率、更易讀。

相關內容