如何防止grep打印尾随换行符? [英] How to prevent grep from printing a trailing newline?
问题描述
我正在使用grep
生成将由另一个程序解析的输出.
I am using grep
to produce output that will be parsed by another program.
但是,该程序期望输出仅是数字或零字节.
However, that program expects output only to be numeric or zero-bytes.
现在grep
在其输出之后输出换行符.我已经检查了-Z
选项,但由于使用grep进行计数(-c
),它似乎不起作用.
Now grep
outputs a newline character after its output. I've checked the -Z
option but it doesn't seem to work as I'm using grep for counting (-c
).
我在sh
中执行,而不是在bash
中执行.因此,将其嵌套到echo -n "$(grep -c pattern)"
也不起作用.
I am executing in sh
, not bash
. So nesting it into echo -n "$(grep -c pattern)"
doesn't work either.
如何摆脱尾随的换行符?
How can I get rid off the trailing newline?
推荐答案
您可以通过tr
将其管道传输并将\n
转换为\0
字符:
You can pipe it through tr
and translate the \n
to a \0
character:
tr '\n' '\0'
这篇关于如何防止grep打印尾随换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!