Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

If I nano two files, one of which reads 'this' without me entering a newline, and one of which reads 'is' without me entering a newline, I want to be able to then cat the two files together into something like 'thisis'.

Instead, the newlines are inserted automatically.

alec@ROOROO:~/$ cat test1 test2 > test3
alec@ROOROO:~/$ cat test3
this
is

So, how can I concatenate two files without adding a newline?

share|improve this question
add comment

4 Answers

up vote 11 down vote accepted

Those trailing newlines are added by nano, not by cat.

Use nano's -L parameter:

-L (--nonewlines)
    Don't add newlines to the ends of files.

Or ~/.nanorc's nonewlines command:

set/unset nonewlines
    Don't add newlines to the ends of files.
share|improve this answer
add comment

If that's happening, you have most certainly inserted a newline character youself. cat concatenates them, just like it always does. You can test this by using cat to first write and then concatenate:

cat > file1
# write something
# hit Ctrl+D twice to end file
# repeat steps with file2
cat file1 file2
share|improve this answer
add comment

cat does not add newlines. The newline is already present at the end of test1. This is normal: a text file consists of a sequence of lines, and a line consists of a sequence of printable characters followed by a newline character. Thus all non-empty text files end with a newline character.

If you have two text files, and you want to concatenate them together, use cat. You'll get the lines of the first file followed by the lines of the second file.

If you want to do something more complicated, viz, join the last line of the first file with the first line of the second file, you need a more complicated command. For example, you can strip the last character of the first file, and append the second file. With GNU coreutils (i.e. on non-embedded Linux), you can do this:

{ <test1 head -c -1 && cat test2; } >test3

or in two steps:

<test1 head -c 1 >test3 && <test2 cat >>test3
share|improve this answer
    
Question: Isn't there a UUoC in the two examples you're giving? I understand perfectly what it does, it's printing the content of the file to stdout. However I naively believe that cat should be used to concatenate only. The fact that you're redirecting test2 to stdin instead of giving it as an argument doesn't change much: you're using cat on a single file. Am I off thinking this way? and how would you proceed to avoid using cat on single files? –  rahmu May 3 '12 at 5:39
    
@rahmu UUoC is when you pipe cat into another command or when you pipe another command into cat. Here, I'm doing neither. My use of cat is a useful one: how else would you copy input to output? (Yes, there are other commands that can do this, but there's no reason to prefer them over cat.) And the question was about cat anyway. –  Gilles May 3 '12 at 10:07
add comment

One way:

paste -d'\0' test1 test2

Output:

thisis
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.