Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

sed

xargs

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?

like image 401
bstpierre Avatar asked Sep 20 '10 23:09

bstpierre


3 Answers

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.

like image 110
Idcmp Avatar answered Nov 05 '22 11:11

Idcmp


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
like image 35
Dennis Williamson Avatar answered Nov 05 '22 11:11

Dennis Williamson


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

like image 1
Ole Tange Avatar answered Nov 05 '22 12:11

Ole Tange