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"

더 짧지는 않더라도 더 휴대하기 쉽고, 더 효율적이고, 더 읽기 쉽습니다.

관련 정보