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
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
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
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
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.
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.