Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Is it possible to make xargs use only newline as separator? (in bash on Linux and OS X if that matters)

I know -0 can be used, but it's PITA as not every command supports NUL-delimited output.

share|improve this question
    
What is it that you're trying to accomplish. There may be another way to go about it. –  Dennis Williamson Mar 31 '10 at 4:50
    
@Dennis I use it for various things, often in pipeline with grep, sed, very basic awk etc., mostly to parallelize execution. find -print0 -name \*.foo -maxdepth 1 | xargs -0 -P4 is way too much to type compared with ls *.foo | xargs -P4. –  porneL Mar 31 '10 at 13:21
    
Scripts and functions are two excellent ways to reduce typing. –  Dennis Williamson Mar 31 '10 at 13:49
    
This is an SO question, isn't it? –  Charles Stewart Apr 1 '10 at 11:22

6 Answers 6

up vote 10 down vote accepted

Something along the lines of

alias myxargs='perl -p -e "s/\n/\0/;" | xargs -0'
cat nonzerofile | myxargs command

should work.

share|improve this answer
18  
tr '\n' '\0' works too. –  porneL Apr 5 '10 at 15:26

GNU xargs (default on Linux, install findutils from MacPorts on OS X to get it) supports -d which lets you specify a custom delimiter for input, so you can do

ls *foo | xargs -d '\n' -P4 foo 
share|improve this answer
    
I have alias xxargs="xargs -d '\n'" in my bashrc. So I can just do things like this: grep -IRl foo | xxargs sed -i s/foo/bar/g –  tylerl Feb 13 '11 at 0:45
1  
This really should be the accepted answer. No need to break out perl. Though tr is a good idea too. –  Ehtesh Choudhury Dec 3 '12 at 18:36
    
Yes I also agree this answer is the correct one. –  Sridhar-Sarnobat Oct 6 '13 at 1:07

With Bash, I generally prefer to avoid xargs for anything the least bit tricky, in favour of while-read loops. For your question, while read -ar LINE; do ...; done does the job (remember to use array syntax with LINE, e.g., ${LINE[@]} for the whole line). This doesn't need any trickery: by default read uses just \n as the line terminator character.

I should post a question on SO about the pros & cons of xargs vs. while-read loops... done!

share|improve this answer

I think you will find that GNU Parallel http://www.gnu.org/software/parallel/ solves both your problems with newline and with running jobs in parallel.

Watch the intro video: http://www.youtube.com/watch?v=OpaiGYxkSuQ

share|improve this answer

Not with the standard version. I have a hacked version that does that - I used it before I knew about 'find ... -print0 | xargs -0 ...'. Contact me if you want a copy - see my profile. (No rocket science - it just about does the job, though. It is not a complete xargs replica.)

share|improve this answer

What about cat file | xargs | sed 's/ /\n/ig' this will convert spaces to newlines, using standard Linux bash tools.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.