LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 05-25-2007, 02:46 AM   #1
nickvk
LQ Newbie
 
Registered: May 2007
Posts: 2

Rep: Reputation: 0
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.
 
Old 05-25-2007, 03:03 AM   #2
nfisk
Member
 
Registered: Jul 2002
Location: Stockholm, Sweden
Distribution: Kubuntu, Fedora
Posts: 45

Rep: Reputation: 15
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
 
Old 05-25-2007, 03:11 AM   #3
nickvk
LQ Newbie
 
Registered: May 2007
Posts: 2

Original Poster
Rep: Reputation: 0
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.
 
Old 05-27-2007, 12:18 PM   #4
nfisk
Member
 
Registered: Jul 2002
Location: Stockholm, Sweden
Distribution: Kubuntu, Fedora
Posts: 45

Rep: Reputation: 15
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
joining .ts and .mkv files Chris594 Linux - Newbie 2 01-17-2007 11:44 PM
joining fileds from separete files dominant Linux - Newbie 5 10-22-2006 01:01 PM
joining text files. chunky Programming 1 05-19-2004 10:42 AM
Joining *wmv files ? gazza Linux - Newbie 3 01-20-2004 12:48 PM
cat - joining files u02gtt Linux - Newbie 6 04-22-2002 08:49 AM


All times are GMT -5. The time now is 01:38 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration