3

I have n files containing one line and want to concatenate them:

Input files:

file_1
A B C

file_2 
1 2 3 

Desired console ouput result:

A B C
1 2 3 

But with:

$ cat file_1 file_2 

I get:

A B C1 2 3 
sjas
  • 18,644
  • 14
  • 87
  • 92
shaq
  • 187
  • 4
  • 13

5 Answers5

5

Try

echo | cat file_1 - file_2

Alternatively, terminate the last line of file_1 with a new-line symbol.

Yet another way:

$ echo > n
$ cat file_1 n file_2 n file_1 n file_2 n
A B C
1 2 3
A B C
1 2 3
Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271
2

If you have more than 2 files, you can use a loop with the shell and use echo to insert a new line:

for f in file1 file2 file3; do cat "$f"; echo; done > output
Bruno
  • 119,590
  • 31
  • 270
  • 376
0

If you only have two files, you could use echo to output a new line and put that between your two files like so:

echo | cat file1 - file2
PoByBolek
  • 3,775
  • 3
  • 21
  • 22
0

Here is what i observe using bash shell on Ubuntu 12.04.

$ echo "1 2 3" > file1
$ echo "a b c" > file2
$ cat file1 file2
1 2 3
a b c

Separate distinct lines.

It looks like echo command ensures that proper terminated strings are written to both the files.

TheCodeArtist
  • 21,479
  • 4
  • 69
  • 130
0

Your file1 is missing a newline at the end. So when you cat them, there's no newline printed to separate file1 from file2.

You either need to modify file1 to include a trailing newline or insert it in some other way.

Emil Sit
  • 22,894
  • 7
  • 53
  • 75