开发者

Change sed line separator to NUL to act as "xargs -0" prefilter?

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开发者_StackOverflow社区 use xargs -0.

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


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.


Pipe it through grep:

filename_listing_command | grep -vzZ '\.py$' | 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:

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


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$' | parallel ac

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


With help of Tom Hale and that answer we have: sed -nzE "s/^$PREFIX(.*)/\1/p"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜