格式化所有的XML文件的目录,并将其保存在一个子目录 [英] Format all XML files in a directory and save them in a subdirectory

查看:146
本文介绍了格式化所有的XML文件的目录,并将其保存在一个子目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个脚本,将搜索目录,找到所有的XML文件,通过 xmllint 运行它们,并格式化的结果保存到一个文件在一个名为的子目录名称相同的格式化。这里的剧本我到目前为止有:

I'm trying to write a script that will look through a directory, find all the XML files, run them through xmllint, and save the formatted results to a file of the same name in a subdirectory called formatted. Here's the script I have so far:

find . -maxdepth 1 -type f -iname "*.xml" | xargs -I '{}' xmllint --format '{}' > formatted/'{}'

这工作,在一定程度上。子目录结束了一个文件,命名为{},它是通过 xmllint 。我怎样才能得到这些文件来正确写入子目录?

This works, to an extent. The subdirectory ends up with one file, named "{}", which is just the results of the final file that was processed through xmllint. How can I get the files to write properly to the subdirectory?

推荐答案

名为 {} ,你看到或许应该包含的所有的该格式的文件一起。这样做的原因是,您正在使用重定向是不实际的的xargs 看到该命令的一部分。该重定向间$ P $由外壳PTED,所以它的作用是执行

The file named {} that you see should probably contain all of the formatted files together. The reason for this is that the redirection that you are using is not actually a part of the command that xargs sees. The redirection is interpreted by the shell, so what it does is run

find . -maxdepth 1 -type f -iname "*.xml" | xargs -I '{}' xmllint --format '{}'

和输出保存到指定的文件格式化/ {}

and save the output to the file named formatted/{}.

尝试使用 - xmllint 输出选项,而不是重定向:

Try using the --output option of xmllint instead of the redirection:

... | xargs -I '{}' xmllint --format '{}' --output formatted/'{}'

您还可以避免使用 -exec 选项找到调用的xargs

You can also avoid calling xargs by using the -exec option of find:

find . -maxdepth 1 -type f -iname "*.xml" -exec xmllint --format '{}' --output formatted/'{}' \;

这篇关于格式化所有的XML文件的目录,并将其保存在一个子目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆