I want to join output of the xargs output by new lines. I do this:

find . -name '*.txt' | xargs -n 1 iconv -f UTF-16 | ...other-commands...

I take one file at a time and convert it to UTF-8 (the system locale). All of the *.txt are one-liners without newline character at the end. So the output of xargs is a mess of text.

How do you separate items of xargs output by \n?


An ugly solution:

find . -name '*.txt' | { xargs -n 1 -I_ bash -c 'iconv -f UTF-16 _;echo '; }| ...other-commands...
  • Ok, thank you. It works just fine. find . -name '*.txt' | {xargs -n 1 -I% bash -c 'iconv -f UTF-16 %; echo;'} – Paul Kole Feb 11 '13 at 18:56

You could try:

find . -name '*.txt' | (xargs -n 1 iconv -f UTF-16; echo; ) | ...other-commands...

That should add a newline after the output of xargs, before piping to the other commands.

  • Sorry, it doesn't work like this. – Paul Kole Feb 11 '13 at 18:47
  • Ah I see where I went wrong. ( created a subshell, { would probably have worked (like the other answer). – lsd Feb 11 '13 at 19:45

Using GNU Parallel you could have done:

find . -name '*.txt' | parallel -k "iconv -f UTF-16 {}; echo" | ...other-commands...

As an added bonus the iconvs would run in parallel.

Watch the intro videos to learn more: https://www.youtube.com/playlist?list=PL284C9FF2488BC6D1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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