Skip to main content
replaced http://unix.stackexchange.com/ with https://unix.stackexchange.com/
Source Link

As Peter says, you first file does not have an end-of-line character. You can probably check it with ls -l --- if it's exactly three chars, this is it.

If you want to "cat" the files adding a newline only if the newline wasn't there, you can use the nice trick explained herehere.

If you have this three files:

[romano:~/tmp] % ls -l f1 f2 f3
-rw-rw-r-- 1 romano romano 3 Jul 12 14:58 f1
-rw-rw-r-- 1 romano romano 4 Jul 12 15:03 f2
-rw-rw-r-- 1 romano romano 4 Jul 12 15:03 f3
[romano:~/tmp] % cat f1 f2 f3
ABCDEF
GHI

where f1 has no end-of-line in the last line, while the others do, you can just do:

[romano:~/tmp] % sed -e '$a\' f1 f2 f3
ABC
DEF
GHI

... sed is a Stream EDitor, and you are instructing it to print all unchanged and the last line adding nothing --- but sed implicitly add a newline when operates, so it solves the problem.

Notice that just using the cat + echo thing will add a newline always. so you'll have two where you had one:

[romano:~/tmp] % for i in f?; do cat $i; echo; done;
ABC
DEF

GHI

[romano:~/tmp] %

As Peter says, you first file does not have an end-of-line character. You can probably check it with ls -l --- if it's exactly three chars, this is it.

If you want to "cat" the files adding a newline only if the newline wasn't there, you can use the nice trick explained here.

If you have this three files:

[romano:~/tmp] % ls -l f1 f2 f3
-rw-rw-r-- 1 romano romano 3 Jul 12 14:58 f1
-rw-rw-r-- 1 romano romano 4 Jul 12 15:03 f2
-rw-rw-r-- 1 romano romano 4 Jul 12 15:03 f3
[romano:~/tmp] % cat f1 f2 f3
ABCDEF
GHI

where f1 has no end-of-line in the last line, while the others do, you can just do:

[romano:~/tmp] % sed -e '$a\' f1 f2 f3
ABC
DEF
GHI

... sed is a Stream EDitor, and you are instructing it to print all unchanged and the last line adding nothing --- but sed implicitly add a newline when operates, so it solves the problem.

Notice that just using the cat + echo thing will add a newline always. so you'll have two where you had one:

[romano:~/tmp] % for i in f?; do cat $i; echo; done;
ABC
DEF

GHI

[romano:~/tmp] %

As Peter says, you first file does not have an end-of-line character. You can probably check it with ls -l --- if it's exactly three chars, this is it.

If you want to "cat" the files adding a newline only if the newline wasn't there, you can use the nice trick explained here.

If you have this three files:

[romano:~/tmp] % ls -l f1 f2 f3
-rw-rw-r-- 1 romano romano 3 Jul 12 14:58 f1
-rw-rw-r-- 1 romano romano 4 Jul 12 15:03 f2
-rw-rw-r-- 1 romano romano 4 Jul 12 15:03 f3
[romano:~/tmp] % cat f1 f2 f3
ABCDEF
GHI

where f1 has no end-of-line in the last line, while the others do, you can just do:

[romano:~/tmp] % sed -e '$a\' f1 f2 f3
ABC
DEF
GHI

... sed is a Stream EDitor, and you are instructing it to print all unchanged and the last line adding nothing --- but sed implicitly add a newline when operates, so it solves the problem.

Notice that just using the cat + echo thing will add a newline always. so you'll have two where you had one:

[romano:~/tmp] % for i in f?; do cat $i; echo; done;
ABC
DEF

GHI

[romano:~/tmp] %
Source Link
Rmano
  • 3.5k
  • 5
  • 24
  • 37

As Peter says, you first file does not have an end-of-line character. You can probably check it with ls -l --- if it's exactly three chars, this is it.

If you want to "cat" the files adding a newline only if the newline wasn't there, you can use the nice trick explained here.

If you have this three files:

[romano:~/tmp] % ls -l f1 f2 f3
-rw-rw-r-- 1 romano romano 3 Jul 12 14:58 f1
-rw-rw-r-- 1 romano romano 4 Jul 12 15:03 f2
-rw-rw-r-- 1 romano romano 4 Jul 12 15:03 f3
[romano:~/tmp] % cat f1 f2 f3
ABCDEF
GHI

where f1 has no end-of-line in the last line, while the others do, you can just do:

[romano:~/tmp] % sed -e '$a\' f1 f2 f3
ABC
DEF
GHI

... sed is a Stream EDitor, and you are instructing it to print all unchanged and the last line adding nothing --- but sed implicitly add a newline when operates, so it solves the problem.

Notice that just using the cat + echo thing will add a newline always. so you'll have two where you had one:

[romano:~/tmp] % for i in f?; do cat $i; echo; done;
ABC
DEF

GHI

[romano:~/tmp] %