Linux 过滤输出并附加到行内的文件

Linux 过滤输出并附加到行内的文件,linux,bash,shell,Linux,Bash,Shell,我有以下输出: {"success":true,"results":2,"total":2,"more":false,"offset":0,"hits":[{"path":"/content/dam/fr/fr-fr/frontend-testing/content-components","excerpt":&quo

我有以下输出:

{"success":true,"results":2,"total":2,"more":false,"offset":0,"hits":[{"path":"/content/dam/fr/fr-fr/frontend-testing/content-components","excerpt":"","name":"content-components","title":"Content Components","lastModified":"2019-05-16 16:01:29","created":"2020-08-19 05:13:27"},{"path":"/content/dam/ae/ae-ar/hidden/products","excerpt":"","name":"products","title":"products","created":"2020-08-19 05:14:44"}]}
我只需要过滤路径后的部分:我成功地过滤了”

现在对我来说最棘手的部分是,我必须将此输出附加到一个文件中,该文件的内容是:

<?xml version="1.0" encoding="UTF-8"?>
<workspaceFilter version="1.0">
    <filter root="/apps/aem65-logistics-content-migration" mode="replace" />
    <filter root="/var/groovyconsole/scripts/aecu/logistics" mode="replace" />
     <filter root="****HERE IS WHERE THE output from the previous query must go****" mode="replace" />
</workspaceFilter>

因此,每次都应该创建一个新行,其中包含查询的输出过滤器。 我不知道bash会怎样,这就是我寻求帮助的原因。

我做了些什么,但到目前为止还没有工作:

sed '' "/<workspaceFilter version="1.0">/a \<filter root="$OUTPUT" mode="replace" > " filter.xml
sed''“//a\”filter.xml
但新行也需要与其他“根”位于同一行 我得到

sed: />/>/a \<filter root="/content/dhl/fr/fr-fr/frontend-testing/content-components": No such file or directory
sed: "/content/dhl/ae/ae-ar/hidden/products" mode="replace" > : No such file or directory
<?xml version="1.0" encoding="UTF-8"?>
<workspaceFilter version="1.0">
    <filter root="/apps/aem65-logistics-content-migration" mode="replace" />
    <filter root="/var/groovyconsole/scripts/aecu/logistics" mode="replace" />
</workspaceFilter
sed://>//a\:没有这样的文件或目录

您可以对it和awk使用两个命令。第一个命令帮助您过滤json文件的内容,然后,您可以使用awk逐行分析以生成xml内容

$ jq .hits[].path a.out | awk '{print \
  "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \
   <workspaceFilter version=\"1.0\"> \
      <filter root=\"/apps/aem65-logistics-content-migration\" mode=\"replace\" /> \
      <filter root=\"/var/groovyconsole/scripts/aecu/logistics\" mode=\"replace\" /> \
      <filter root="$1" mode=\"replace\" /> \
   </workspaceFilter> \
  "}'

欢迎来到SO,on,SO,我们鼓励用户在他们的问题中以代码的形式添加他们的努力,所以请在您的问题中添加相同的内容,然后让我们知道。这是有效的,关于第二部分和附录
$ jq .hits[].path a.out | awk '{print \
  "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \
   <workspaceFilter version=\"1.0\"> \
      <filter root=\"/apps/aem65-logistics-content-migration\" mode=\"replace\" /> \
      <filter root=\"/var/groovyconsole/scripts/aecu/logistics\" mode=\"replace\" /> \
      <filter root="$1" mode=\"replace\" /> \
   </workspaceFilter> \
  "}'
{
 "success": true,
 "results": 2,
 "total": 2,
 "more": false,
 "offset": 0,
 "hits": [
   {
    "path": "/content/dam/fr/fr-fr/frontend-testing/content-components",
    "excerpt": "",
    "name": "content-components",
    "title": "Content Components",
    "lastModified": "2019-05-16 16:01:29",
    "created": "2020-08-19 05:13:27"
   },
   {
    "path": "/content/dam/ae/ae-ar/hidden/products",
    "excerpt": "",
    "name": "products",
    "title": "products",
    "created": "2020-08-19 05:14:44"
   }
 ]
}