如何使用jq替换'\ n'替换换行符

时间:2018-04-19 21:38:35

标签: json shell newline jq

有没有办法让jq用字符'\n'替换命令输出中的实际换行符?

例如,我有以下JSON:

{
    "stderr": "Traceback (most recent call last):\n  File \"download.py\", line 157, in <module>\n    cli()\n  File \"/var/task/click/core.py\", line 722, in __call__\n    return self.main(*args, **kwargs)\n  File \"/var/task/click/core.py\", line 697, in main\n    rv = self.invoke(ctx)\n  File \"/var/task/click/core.py\", line 895, in invoke\n    return ctx.invoke(self.callback, **ctx.params)\n  File \"/var/task/click/core.py\", line 535, in invoke\n    return callback(*args, **kwargs)\n  File \"download.py\", line 77, in cli\n    pool.map(lambda f: f.download(log_level=inspector_log_level), files_to_download)\n  File \"/var/lang/lib/python3.6/multiprocessing/pool.py\", line 260, in map\n    return self._map_async(func, iterable, mapstar, chunksize).get()\n  File \"/var/lang/lib/python3.6/multiprocessing/pool.py\", line 608, in get\n    raise self._value\n  File \"/var/lang/lib/python3.6/multiprocessing/pool.py\", line 119, in worker\n    result = (True, func(*args, **kwds))\n  File \"/var/lang/lib/python3.6/multiprocessing/pool.py\", line 44, in mapstar\n    return list(map(*args))\n  File \"download.py\", line 77, in <lambda>\n    pool.map(lambda f: f.download(log_level=inspector_log_level), files_to_download)\n  File \"/var/task/src/keepout.py\", line 63, in download\n    self.tempfile = mgr.download_url(self.url_or_path, suffix=file_extension)\n  File \"/var/task/src/download.py\", line 24, in download_url\n    shutil.copyfileobj(fsrc, fdst, length=16 * 1024)\n  File \"/var/lang/lib/python3.6/shutil.py\", line 82, in copyfileobj\n    fdst.write(buf)\n  File \"/var/lang/lib/python3.6/tempfile.py\", line 483, in func_wrapper\n    return func(*args, **kwargs)\nOSError: [Errno 28] No space left on device\n"
}

如果我可以通过jq管道并查看使用实际换行符格式化的字段的值,我会很高兴,例如

{
    "stderr": "Traceback (most recent call last):
  File \"download.py\", line 157, in <module>
    cli()
  File \"/var/task/click/core.py\", line 722, in __call__
    return self.main(*args, **kwargs)
  File \"/var/task/click/core.py\", line 697, in main
    rv = self.invoke(ctx)
  File \"/var/task/click/core.py\", line 895, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File \"/var/task/click/core.py\", line 535, in invoke
    return callback(*args, **kwargs)
  File \"download.py\", line 77, in cli
    pool.map(lambda f: f.download(log_level=inspector_log_level), files_to_download)
  File \"/var/lang/lib/python3.6/multiprocessing/pool.py\", line 260, in map
    return self._map_async(func, iterable, mapstar, chunksize).get()
  File \"/var/lang/lib/python3.6/multiprocessing/pool.py\", line 608, in get
    raise self._value
  File \"/var/lang/lib/python3.6/multiprocessing/pool.py\", line 119, in worker
    result = (True, func(*args, **kwds))
  File \"/var/lang/lib/python3.6/multiprocessing/pool.py\", line 44, in mapstar
    return list(map(*args))
  File \"download.py\", line 77, in <lambda>
    pool.map(lambda f: f.download(log_level=inspector_log_level), files_to_download)
  File \"/var/task/src/keepout.py\", line 63, in download
    self.tempfile = mgr.download_url(self.url_or_path, suffix=file_extension)
  File \"/var/task/src/download.py\", line 24, in download_url
    shutil.copyfileobj(fsrc, fdst, length=16 * 1024)
  File \"/var/lang/lib/python3.6/shutil.py\", line 82, in copyfileobj
    fdst.write(buf)
  File \"/var/lang/lib/python3.6/tempfile.py\", line 483, in func_wrapper
    return func(*args, **kwargs)
OSError: [Errno 28] No space left on device
"
}

有办法做到这一点吗?

0 个答案:

没有答案