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
  • 17,071
  • 12
  • 81
  • 86
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
  • 125,859
  • 15
  • 164
  • 254
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
  • 115,278
  • 29
  • 262
  • 368
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,615
  • 3
  • 19
  • 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
  • 20,467
  • 4
  • 64
  • 128
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,052
  • 7
  • 50
  • 75