Для редактирования всех других (каждая вторая) строка, начиная с первого, с GNU SED
, выполните
sed '1~2s/^/>/' your_file
Это напишет модифицированный файл на стандартный выход Отказ
I.E., если вы напечатаете , только вышеупомянутый, модифицированный файл будет отображаться на экране.
Вы можете поместить это в новый файл через , перенаправляющий вывод с >
; E.G.,
sed '1~2s/^/>/' your_file > your_new_file
Или, если вы хотите изменить существующий файл, используйте -I
:
sed -i '1~2s/^/>/' your_file
Как я сказал в своем комментарии, я не понимаю, что на самом деле у вас за вопрос. Вот несколько более лаконичных способов сделать то, что делает ваш скрипт 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>
Учитывая фиксированное начало (записанное в файле xml_head) и конец вывода (xml_tail)
.
и переменной $a:
cat xml_head;echo "$a"; cat xml_tail
Также можно сделать шаблон xml_file с ===a=== посередине:
cat xml_template | sed 's/===a===/'$a'/'
Однако последнее решение будет неудачным, когда в $a появятся новые строки.
На самом деле, в переменной $a входных данных нет, а есть входной/выходной файл. Решение должно быть что-то вроде
mv ${file} ${file}.org && \
(cat xml_head && pr -tro 6 ${file}.org && cat xml_tail) > ${file} && rm ${file}.org
Не более лаконично, но, возможно, более читабельно:
tmp=$(mktemp)
cat <<END >$tmp && mv $tmp "$1"
<?xml version="1.0" encoding="utf-8"?>
<hello>
<world>
$(sed 's/^/ /' "$1")
</world>
</hello>
END
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
{
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"
Будет более портативным, эффективным и читабельным, если не короче.