66

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.

masegaloeh
  • 17,978
  • 9
  • 56
  • 104
Kornel
  • 1,075
  • 1
  • 11
  • 16

6 Answers6

77

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 
StackzOfZtuff
  • 1,754
  • 12
  • 21
James
  • 7,553
  • 2
  • 24
  • 33
23

Something along the lines of

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

should work.

hurfdurf
  • 933
  • 7
  • 11
6

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!

Charles Stewart
  • 650
  • 6
  • 19
  • 2
    This can only emulate `xargs -n1 -d'\n'`, and not the whole `xargs`. – Hello World Apr 23 '15 at 20:12
  • @HelloWorld - You can bunch arguments together easily enough, so you can simulate `xargs -n$N` as well with only a bit of extra scripting. – Charles Stewart May 05 '15 at 16:33
  • Yes, I'm among the upvoters, it's just a remark worth mentioning. – Hello World May 09 '15 at 10:43
  • This is exactly what I do! Plus, you can use the `read` options, like reading more words in separate variables, or change delimiter with `IFS=' '`. – caesarsol Mar 13 '17 at 14:14
  • Reading the line into an array when you're expecting single lines is overkill at best, but more likely bug-inducing. When Bash reads into an array, it splits input on `$IFS` (normally whitespace), and concatenating the array with `${LINE[*]}` or `${LINE[@]}` does not restore that original whitespace. Using `while read -r line; do ...` is better for most situations, and lowercase variables avoid conflict with built-in/environment variables. – Walf Oct 26 '21 at 02:56
4
$ echo "1\n2 3\n4 5 6" | xargs -L1 echo  "#"
# 1
# 2 3
# 4 5 6
cmcginty
  • 1,263
  • 15
  • 24
2

please use the -d option of xargs

  --delimiter=delim, -d delim

Input items are terminated by the specified character. The specified delimiter may be a single character, a C-style character escape such as \n, or an octal or hexadecimal escape code. Octal and hexadecimal escape codes are understood as for the printf command. Multibyte characters are not supported. When processing the input, quotes and backslash are not special; every character in the input is taken literally. The -d option disables any end-of-file string, which is treated like any other argument. You can use this option when the input consists of simply newline-separated items, although it is almost always better to design your program to use --null where this is possible.

Example:

$ echo 'for arg in "$@"; do echo "arg: <$arg>"; done' > show_args
$ printf "a a\nb b\nc c\n"
a a
b b
c c
$ printf "a a\nb b\nc c\n" | xargs -d '\n' bash show_args
arg: <a a>
arg: <b b>
arg: <c c>
osexp2003
  • 355
  • 3
  • 5
-3

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

Omar Al-Ithawi
  • 180
  • 1
  • 9
  • 2
    If `file` contains spaces and newlines, then the result of this command will make it impossible to distinguish between them, which seems to be going in the opposite direction of what this question asks for – Max Nanasy Sep 24 '18 at 19:07