LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   Joining files (http://www.linuxquestions.org/questions/linux-general-1/joining-files-556557/)

nickvk 05-25-2007 02:46 AM

Joining files
 
Hi,

This sounds really simple, but I just can't work it out.

Let's say I have two files, a.txt and b.txt. I want to join them together into c.txt, but I want the first line of b.txt to continue on from the last line of a.txt

Example:

a.txt contains:
Hello
I want something to be joi


b.txt contains:
ned together with no line break
That would be good


If just do a simple: cat a.txt b.txt > c.txt

I get the following:

Hello
I want something to be joi
ned together with no line break
That would be good


I would like the output to look like the following:
Hello
I want something to be joined together with no line break
That would be good


Does anyone have any idea how I would go about doing this?

Please note that I do want the other line breaks that are meant to be there to be preserved, I just want the start of b.txt to continue on from the last line of a.txt


Cheers,

Nick v K.

nfisk 05-25-2007 03:03 AM

Make sure the first file doesn't end with an end-of-line:

Code:

$ echo -n foo >a.txt
$ echo bar >b.txt
$ cat a.txt b.txt >c.txt
$ cat c.txt
foobar


nickvk 05-25-2007 03:11 AM

Thanks nfisk,

But is there any other way to do this?

I'm after a method that would strip the newline/end of file character off an existing file, which already has a trailing newline character.

Thanks.

Nick v K.

nfisk 05-27-2007 12:18 PM

Well, a crude but effective solution using Perl if a.txt ends with an unwanted newline char:
Code:

$ perl -e 'undef $/; $_ = <STDIN>; s/\n$//; print' <a.txt >new_a.txt
You probably won't want to do that if the file is huge, though, as its entire contents is read into memory all at once.


All times are GMT -5. The time now is 08:44 AM.