问题
I have a folder that contains about 2000 text files in it.
At the top of every file I need these two lines added:
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
And at the bottom of each file I need this added:
</urlset>
Is there any way to massively do this across all the files at once via command line?
Thanks in advance!
回答1:
$ cat header
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
$ cat footer
</urlset>
$ cat header file footer
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
one line
</urlset>
To wrap the above in a bash-loop:
for f in *.xml; do cat header $f footer > tmp && mv tmp $f; done
回答2:
With a one-liner :
sed -i -e '1 i <?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' -e '$s@$@\n</urlset>@' *.xml
来源:https://stackoverflow.com/questions/14967842/add-new-lines-to-top-and-bottom-of-every-text-file