concatenate two files without adding a newline

461

Solution 1

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.

Solution 2

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

Solution 3

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

Solution 4

One way:

paste -d'\0' test1 test2

Output:

thisis
Share:
461

Related videos on Youtube

Ariel
Author by

Ariel

Updated on September 18, 2022

Comments

  • Ariel
    Ariel over 1 year

    Does anyone know whether it is possible to play a video that was synched with iTunes from within an application of your own? In other words - it is of course possible to programmatically create a movie player inside an application, and play movies via HTTP or movies locally stored within the home directory of the application. My question is whether it is possible to access and play a movie that was synched with iTunes, and thus resides elsewhere in the file system.

    Thanks! Ariel

    • Art Gillespie
      Art Gillespie over 13 years
      As of 4.1, MPMediaLibrary only provides access to music, podcasts, and audio books in the iPod library. If enough of us file a bug asking for video access, maybe we'll get it in a future release.
  • McKayla
    McKayla about 13 years
    I thought they changed that in 4.0?
  • hhafez
    hhafez about 13 years
    possibly, it has been over a year since I did anything on iOS
  • rahmu
    rahmu about 12 years
    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?
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' about 12 years
    @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.