소개
스크립트:
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"
더 짧지는 않지만 더 휴대 가능하고 효율적이며 읽기 쉬울 것입니다.