[英]How to pipe an output to printf, when the \n text is not being breaken by bash?
我有这样的文字:
"while compiling ejs\n\nIf the above error is not helpful, you may want to try EJS-Lint:\nhttps://github.com/RyanZim/EJS-Lint%5CnOr, if you meant to create an async function, pass `async: true` as an option.","stack":"SyntaxError: (...)"
在 bash 中没有被分成几行(运行npm run Script
)
我如何将 pipe 转换为 printf 或任何工具,以使 \n 实际呈现为断线?
我尝试在管道文本上查看多个来源,以使 bash 将字符串 \n 读入实际上是一个换行符,但没有成功。
我希望 '\n' 实际上会换行。
正如@Charles Duffy 指出的那样,
npm run Script | while IFS= read -r line; do printf '%b\n' "$line"; done
作品。 最终的 output 看起来像这样,以提供更多的关闭:
If the above error is not helpful, you may want to try EJS-Lint:
https://github.com/RyanZim/EJS-Lint
Or, if you meant to create an async function, pass `async: true` as an option.
at new Function (<anonymous>)
at Template.compile (<ommited-path>/node_modules/ejs/lib/ejs.js:673:12)
at Object.compile (<ommited-path>/node_modules/ejs/lib/ejs.js:398:16)
一个while read
循环来收集行,然后将它们放入printf %b
进行格式化应该做你想做的:
npm run Script | while IFS= read -r line; do printf '%b\n' "$line"; done
BashFAQ #1中记录了此处使用的一般模式。 一些注意事项:
while
循环的退出状态,除非您先运行set -o pipefail
使管道中的任何位置发生故障导致整个命令报告故障。IFS=
阻止删除输入前面或结尾的空格。line
变量之前,使用-r
会阻止反斜杠被read
本身删除。printf '%b\n'
是 bash 以非标准方式实现的POSIX 建议的替代方案,如echo -e
(请参阅链接的“应用程序使用”部分; Why is printf better than echo的答案中也有具体示例? )。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.