Add new lines to top and bottom of every text file

泄露秘密 提交于 2019-12-11 12:38:25

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!