将 sed 行分隔符更改为 NUL 以充当“xargs -0”预过滤器?

发布于 2024-09-24 09:32:29 字数 366 浏览 2 评论 0原文

我正在运行这样的命令行:

filename_listing_command | xargs -0 action_command

其中 filename_listing_command 使用空字节来分隔文件 - 这就是 xargs -0 想要使用的内容。

问题是我想过滤掉一些文件。像这样的事情:

filename_listing_command | sed -e '/\.py/!d' | xargs ac

但我需要使用 xargs -0 。

如何将 sed 想要的行分隔符从换行符更改为 NUL?

I'm running a command line like this:

filename_listing_command | xargs -0 action_command

Where filename_listing_command uses null bytes to separate the files -- this is what xargs -0 wants to consume.

Problem is that I want to filter out some of the files. Something like this:

filename_listing_command | sed -e '/\.py/!d' | xargs ac

but I need to use xargs -0.

How do I change the line separator that sed wants from newline to NUL?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

就是爱搞怪 2024-10-01 09:32:29

如果您已经点击此 SO 来寻找答案,并且正在使用 GNU sed 4.2.2 或更高版本,那么它现在有一个 -z 选项,可以满足 OP 的要求。

If you've hit this SO looking for an answer and are using GNU sed 4.2.2 or later, it now has a -z option which does what the OP is asking for.

如果没结果 2024-10-01 09:32:29

通过 grep 进行管道传输:

filename_listing_command | grep -vzZ '\.py

-z 在输入上接受空终止符,-Z 在输出上生成空终止符,-v 反转匹配(排除)。

编辑:

如果您更喜欢使用sed,请尝试以下操作:

filename_listing_command | sed 's/[^\x0]*\.py\x0//g' | filename_listing_command
| filename_listing_command

-z 在输入上接受空终止符,-Z 在输出上生成空终止符,-v 反转匹配(排除)。

编辑:

如果您更喜欢使用sed,请尝试以下操作:

Pipe it through grep:

filename_listing_command | grep -vzZ '\.py

The -z accepts null terminators on input and the -Z produces null terminators on output and the -v inverts the match (excludes).

Edit:

Try this if you prefer to use sed:

filename_listing_command | sed 's/[^\x0]*\.py\x0//g' | filename_listing_command
| filename_listing_command

The -z accepts null terminators on input and the -Z produces null terminators on output and the -v inverts the match (excludes).

Edit:

Try this if you prefer to use sed:

一页 2024-10-01 09:32:29

如果您的文件名都不包含换行符,那么使用 GNU Parallel 阅读解决方案可能会更容易:

filename_listing_command | grep -v '\.py

了解有关 GNU Parallel 的更多信息 http://www.youtube.com/watch?v=OpaiGYxkSuQ

| parallel ac

了解有关 GNU Parallel 的更多信息 http://www.youtube.com/watch?v=OpaiGYxkSuQ

If none of your file names contain newline, then it may be easier to read a solution using GNU Parallel:

filename_listing_command | grep -v '\.py

Learn more about GNU Parallel http://www.youtube.com/watch?v=OpaiGYxkSuQ

| parallel ac

Learn more about GNU Parallel http://www.youtube.com/watch?v=OpaiGYxkSuQ

盛装女皇 2024-10-01 09:32:29

汤姆·黑尔的帮助下< /a> 和那个我们有答案:
<代码>
sed -nzE "s/^$PREFIX(.*)/\1/p"

With help of Tom Hale and that answer we have:

sed -nzE "s/^$PREFIX(.*)/\1/p"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文