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!
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>
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 doSomething along the lines of
should work.
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!
please use the -d option of xargs
Example:
What about
cat file | xargs | sed 's/ /\n/ig'
this will convert spaces to newlines, using standard Linux bash tools.