0

I want to write a shell script to merge contents of multiple files in a given directories.

DIR1 contains sample1.txt sample2.txt
    sample1.txt contents  :---this is sample1 file
    sample2.txt contents  :---this is sample2 file

DIR2 contains demo1.txt demo2.txt
    demo1.txt contents  :---this is demo1 file

I tried :

(find /home/DIR1  /home/DIR2 -type f | xargs -i cat {} ) > /result/final.txt

It worked!

this is sample2 file  this is sample1 file  this is demo1 file

however output appears in a single line I need every file's output in a separate new line. like this:

this is sample1 file
this is sample2 file  
this is demo1 file

how to achieve this?

Any help would be appreciated in advance.

ashwini
  • 531
  • 5
  • 13
  • 28
  • I am not able to reproduce your issue. Your example works fine in my machine. I don't know why the output packed to single line in your case. – mainframer May 31 '15 at 11:33
  • @mainframer it works differently in his case because the original files don't end with a newline – shx2 May 31 '15 at 17:58

2 Answers2

0

As is pointed out in the comments, the issue may be because the End of File(EOF) is not preceded by the \n (newLine character).

One way of circumventing this issue, is to replace the "--- " with newline character. Hope the following command resolves the issue:

(find DIR1/  DIR2/ -type f | xargs -i cat {} ) | sed "s/$/\r/g" > result/final.txt
envy_intelligence
  • 453
  • 1
  • 6
  • 21
0

Your files don't end with newlines, and therefor there are no newlines in the output file.

You should either make sure your input files end with newlines, or add them in the find command:

find /home/DIR1  /home/DIR2 -type f -exec cat {} \; -exec echo \;
shx2
  • 61,779
  • 13
  • 130
  • 153