Introdução
roteiro:
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
entrada:
<city id="city01">
<name>utrecht</author>
<population>328.577</population>
<districts>10</districts>
<country>netherlands</country>
</city>
saída:
<?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>
Pergunta
Quais são os métodos mais concisos para agrupar um arquivo do que usar sed?
Responder1
Como eu disse no meu comentário, não entendo qual é a sua pergunta real. Aqui estão algumas maneiras mais concisas de fazer o que seu sed
script faz:
$ 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>
ou
$ 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>
ou
$ 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>
Você pode editar o arquivo no local com perl -i -ple
.
ou
$ 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>
ou uma mistura:
$ 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>
Responder2
Não é mais conciso, mas talvez mais legível:
tmp=$(mktemp)
cat <<END >$tmp && mv $tmp "$1"
<?xml version="1.0" encoding="utf-8"?>
<hello>
<world>
$(sed 's/^/ /' "$1")
</world>
</hello>
END
Responder3
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
Responder4
{
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"
Seria mais portátil, eficiente e legível, senão mais curto.