1838613 Members
5338 Online
110128 Solutions
New Discussion

Joining Text Files

 
SOLVED
Go to solution
David Robertson_1
Occasional Contributor

Joining Text Files

How do I join text files with NO newline between them.
I've tried using 'cat file1 file2 file3' but this adds newlines.

Dave Robertson
9 REPLIES 9
Andreas Voss
Honored Contributor
Solution

Re: Joining Text Files

Hi,

this might be not the best solution but it works:

echo $(cat file1)$(cat file2) > file

OR if not ksh/posix-sh

echo `cat file1``cat file2` > file

Regards
Lasse Knudsen
Esteemed Contributor

Re: Joining Text Files

Hi there,

'cat' does not add newlines - my guess is that your NLs are already in the files.
In a world without fences - who needs Gates ?
Ralph Grothe
Honored Contributor

Re: Joining Text Files

I have the sneaking suspicion that the presented solution might fail on utterly large files, based on what echo presumably can swallow.
If I were sure about the redundant n in the last line of each file I'd simply send it through the stream editor (awk, perl, or whatever), like this
$ sed '$d' file1 file2 ... fileN > fileCatted
If unsure about the trailing n better use a regex to filter.
TIMTOWTDI as they would say in Perl-lingo.
But my feeling may be totally errant because I'm relatively new to Unix.
Madness, thy name is system administration
Ralph Grothe
Honored Contributor

Re: Joining Text Files

Ooops, weren't aware of the disappearing of backslashes in the forum.
Of course, substitute \n for n
Madness, thy name is system administration
Ralph Grothe
Honored Contributor

Re: Joining Text Files

Ooops, weren't aware of the disappearing of backslashes in the forum.
Of course, substitute \n for n
Madness, thy name is system administration
Ralph Grothe
Honored Contributor

Re: Joining Text Files

Ooops, weren't aware of the disappearing of backslashes in the forum.
Of course, substitute \n for n
Madness, thy name is system administration
Ralph Grothe
Honored Contributor

Re: Joining Text Files

Ooops, weren't aware of the disappearing of backslashes in the forum.
Of course, substitute \n for n
Madness, thy name is system administration
James R. Ferguson
Acclaimed Contributor

Re: Joining Text Files

David:

See man pages for 'join'. Is this what you seek?

...JRF...
Ralph Grothe
Honored Contributor

Re: Joining Text Files

I have to apologize for my silly postings.
First, the annoying repetitive last posting was due to a webserver failure.
Second, one should first try things before giving advice. ;-)
Third, I think I misunderstood the issue.
If it was for chopping the trailing \n of each line (what the echo from above is doing on my machine) then you could use this Perl oneliner (even the obsolete Perl 4 shipped with HP-UX should do the trick)

$ perl -ne 'chop;print' file1 file2 ... fileN

But of course, this is also easily done using sed or awk.

Madness, thy name is system administration