LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-28-2013, 02:51 PM   #1
ktrampe
LQ Newbie
 
Registered: Jun 2013
Posts: 3

Rep: Reputation: Disabled
Combine two files... without inserting a newline in between them


Hi there.
I'm trying to combine two files using the cat command, but it's inserting a linefeed character (0x0A) in between the two files I'm trying to merge. Is there a flag I can set for the cat command to force it not to do that?

TIA
 
Old 06-28-2013, 03:10 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
What command are you using?

This seems to work:
Code:
cat file1 file2 > file3
I am assuming your are catting text files (shouldn't be an issue).

Last edited by druuna; 06-28-2013 at 03:43 PM. Reason: nuanced assuming part
 
Old 06-28-2013, 03:14 PM   #3
ktrampe
LQ Newbie
 
Registered: Jun 2013
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by druuna View Post
What command are you using?

This seems to work:
Code:
cat file1 file2 > file3
I am assuming your are catting text files.
No, that inserts a 0x0A character between them. I am using very large digital negative files from a canon 5d Mk III.
I feel like this should be a very simple task, like there should be a flag in cat for this, since, you're not really just concatenating if you add data to the two files.

Try it, even with txt files... For example:

date > file1
date > file2
cat file1 file2 > file3

file3 looks like this:
yourcurrentdatehere
yourcurrentdatehere

It should look like this:
yourcurrentdatehereyourcurrentdatehere

... or maybe I'm missing something...
 
Old 06-28-2013, 03:41 PM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Quote:
Originally Posted by ktrampe View Post
No, that inserts a 0x0A character between them. I am using very large digital negative files from a canon 5d Mk III.
I feel like this should be a very simple task, like there should be a flag in cat for this, since, you're not really just concatenating if you add data to the two files.

Try it, even with txt files... For example:

date > file1
date > file2
cat file1 file2 > file3

file3 looks like this:
yourcurrentdatehere
yourcurrentdatehere

It should look like this:
yourcurrentdatehereyourcurrentdatehere

... or maybe I'm missing something...
You are missing something

There is always a last new line character. Using 2 text files:
Code:
$ cat file1 
1
2
3
$ cat file2 
a
b
c
$ hexdump -c file1 
0000000   1  \n   2  \n   3  \n                                        
0000006
$ hexdump -c file2
0000000   a  \n   b  \n   c  \n                                        
0000006
$ cat file1 file2 > file3
$ hexdump -c file3
0000000   1  \n   2  \n   3  \n   a  \n   b  \n   c  \n                
000000c
I never typed the bold parts, they are the end of the file marker in this case.

And that is a good thing, otherwise the output might have looked like this:
Code:
1
2
3a
b
c
 
Old 06-28-2013, 03:46 PM   #5
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,026

Rep: Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845
this c program mite help:
Code:
[schneidz@hyper ~]$ cat ktrampe.c
#include <stdio.h>

main(int argc, char *argv[])
{
 int c;
 FILE * fstream1;
 FILE * fstream2;

 fstream1 = fopen(argv[1], "r");
 fstream2 = fopen(argv[2], "r");

 c = fgetc(fstream1);
 while(c != EOF)
 {
  printf("%c", c);
  c = fgetc(fstream1);
 }

 c = fgetc(fstream2);
 while(c != EOF)
 {
  printf("%c", c);
  c = fgetc(fstream2);
 }
 fclose(fstream1);
 fclose(fstream2);
}
 
Old 06-28-2013, 03:54 PM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
@schneidz: What should be different?

End result seems to be the same:
Code:
./a.out file1 file2 > file3
$ hexdump -c file3
0000000   1  \n   2  \n   3  \n   a  \n   b  \n   c  \n                
000000c
 
1 members found this post helpful.
Old 06-28-2013, 04:06 PM   #7
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,026

Rep: Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845
i tried it with binary files and it seems not to append \n after the first file.
 
Old 06-28-2013, 04:09 PM   #8
ktrampe
LQ Newbie
 
Registered: Jun 2013
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thanks, all, for your help.

"And that is a good thing, otherwise the output might have looked like this:"

That output is EXACTLY what I'm trying to accomplish.

You're right, in my testing I've only ever used text data. If I hexdump the original test files, they have a 0x0A character at the end of the file that I didn't think was there.

So, it seems my problem is not a problem at all. Had I attempted merging the digital negatives instead of some text files, there would have been no problem. What threw me off was that even though there didn't seem to be a newline character at the end of the file in a text editor, there was.


I just merged them and the video is playing seamlessly.



Thanks and sorry for wasting your time!
 
Old 06-29-2013, 10:15 AM   #9
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,599

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
Is ok - as a side note, you call also append files using "cat file >>firstfile", again, nothing can be added to the "firstfile" except the contents of the "file".

Sometimes this form of appending is more useful, as it allows you to check things as you go.
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Add newline EOF (.gz files) BarataPT Linux - Newbie 2 03-11-2011 11:58 AM
cat 2 files without newline lea Programming 5 12-23-2010 04:54 PM
Concatenating two files without creating a newline between them? linux_newestbie Linux - General 8 03-12-2010 09:16 AM
Inserting Multiple Lines (with newline) using sed or awk hal8000b Programming 1 03-08-2009 06:21 PM
Best way to combine files? JockVSJock Programming 4 04-25-2004 08:38 PM


All times are GMT -5. The time now is 04:23 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration