tags:

views:

46

answers:

2

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?

+1  A: 

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
Dennis Williamson
Nice, thanks. I didn't know about `grep -zZ`. So is the answer on sed: "you can't do that"?
bstpierre
@bstpierre: See my edit.
Dennis Williamson
@Dennis Williamson: great, thanks!
bstpierre
+1  A: 

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

Ole Tange