如何防止grep打印尾随换行符? [英] How to prevent grep from printing a trailing newline?

查看:113
本文介绍了如何防止grep打印尾随换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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屋!

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