Fedora Linux Support Community & Resources Center
  #1  
Old 31st May 2012, 02:50 AM
griffinmt Offline
Registered User
 
Join Date: Oct 2005
Location: Central Michigan
Age: 64
Posts: 190
windows_7ie
Question Concatenating files

I have several text files that I want to append to each other into a single larger file. What is the correct method for doing this?
__________________
Martyn Griffin
Reply With Quote
  #2  
Old 31st May 2012, 03:03 AM
jims Offline
Registered User
 
Join Date: Oct 2006
Location: Richmond BC Canada
Posts: 106
linuxfirefox
Re: Concatenating files

Take a look at cat (see 'man cat'). It will handle multiple files and copy them to an output file in the order in which the input files are specified.
Reply With Quote
  #3  
Old 31st May 2012, 03:03 AM
griffinmt Offline
Registered User
 
Join Date: Oct 2005
Location: Central Michigan
Age: 64
Posts: 190
windows_7ie
[SOLVED] Concatenating files

Sorry about that, another brain f**t happened.

Seems obvious now that the cat command is what I want!!!
__________________
Martyn Griffin
Reply With Quote
  #4  
Old 31st May 2012, 05:00 PM
Gareth Jones Offline
Official Gnome 3 Sales Rep. (and Adminstrator)
 
Join Date: Jul 2011
Location: Leamington Spa, UK
Age: 31
Posts: 2,750
linuxfirefox
Re: Concatenating files

cat is probably one of the most abused commands in Unix, rarely used for its intended purpose, and often used completely unnecessarily...
Reply With Quote
  #5  
Old 31st May 2012, 05:35 PM
Adunaic Offline
Registered User
 
Join Date: Mar 2009
Location: Lancaster, UK
Posts: 907
linuxfirefox
Re: Concatenating files

Quote:
Originally Posted by Gareth Jones View Post
cat is probably one of the most abused commands in Unix, rarely used for its intended purpose, and often used completely unnecessarily...
Am i not meant to write scripts with it?
Reply With Quote
  #6  
Old 31st May 2012, 10:42 PM
Doug G Offline
Registered User
 
Join Date: Jul 2005
Posts: 640
linuxfirefox
Re: Concatenating files

Quote:
Originally Posted by Adunaic View Post
Am i not meant to write scripts with it?
Don't worry about it, use cat however you need to in scripts or from the command line.

---------- Post added at 02:42 PM ---------- Previous post was at 02:40 PM ----------

Quote:
Originally Posted by Gareth Jones View Post
cat is probably one of the most abused commands in Unix, rarely used for its intended purpose, and often used completely unnecessarily...
Using cat unnecessarily, or not for it's intended purpose must be because those who claim to know how to use cat don't tell others ...

Hey, I see this forum code assumed I wanted to just make a single post, even if I wanted two separate replies.
.
__________________
======
Doug G
======
Reply With Quote
  #7  
Old 31st May 2012, 11:11 PM
stevea Offline
Registered User
 
Join Date: Apr 2006
Location: Ohio, USA
Posts: 8,547
linuxfirefox
Re: Concatenating files

I suspect Gareth is just saying that a lot of times ppl insert an extra cat in a command sequence where proper I/O redirection with '<' and '>' etc would do as will with one less process. silly things like;

grep " / " /etc/fstab | cat >myrootmount.txt
vs
grep " / " /etc/fstab >myrootmount.txt


There is no magical knowledge about cat. It's one of the simplest utilities out there. I takes a list of filenames from its arglist, opens each in order, and copies their content to stdout. If there is no filename in the arglist, then it copies stdin to stdout (which is usually superfluous).

Old cat (decades ago) only had a '-v' option to print non-printable characters in some decent mode.
Modern (GNU) cat additionally has options to delete blank lines or to number output lines.

So a good reason to use cat is to copy MULTIPLE files to a pipe(stdout) or to view the content of a short text file. It to perform the trivial text processing it supports (line numbers, conversion of unprintables) Frankly 'less' has better utility for text viewing.

Bad reasons are -
- to redirect stdin to stdout (pointless)
- copy a single file to a pipe (use redirection instead)
__________________
None are more hopelessly enslaved than those who falsely believe they are free.
Johann Wolfgang von Goethe

Last edited by stevea; 31st May 2012 at 11:26 PM.
Reply With Quote
  #8  
Old 31st May 2012, 11:29 PM
smr54 Online
Registered User
 
Join Date: Jan 2010
Posts: 5,389
linuxchrome
Re: Concatenating files

A typical misuse of cat. Note that this isn't horrible (especially with a small file) but it's the sort of thing that will happen when someone who is a beginner helps another beginner.

cat myfile|grep testing

A more proper way to do it would be

grep testing myfile

That's a really simple example, but it's the sort of thing that happens when one is learning---they think that they have to cat the file to grep something.

http://partmaps.org/era/unix/award.html#uucaletter might be fun reading. (relatively short)
Reply With Quote
  #9  
Old 31st May 2012, 11:48 PM
CronoCloud Offline
Registered User
 
Join Date: May 2010
Location: Midwest USA
Posts: 420
linuxfirefox
Re: Concatenating files

Quote:
Originally Posted by smr54 View Post
but it's the sort of thing that will happen when someone who is a beginner helps another beginner.
That's a really simple example, but it's the sort of thing that happens when one is learning---they think that they have to cat the file to grep something.
I think the reason that happens is that some Linux books for beginners use the first example as an example of how to use pipes and grep in a basic manner. I've used the first one many many times...until I was told that grep can grep files directly as well as accepting pipes and redirection.

CronoCloud
Reply With Quote
  #10  
Old 31st May 2012, 11:59 PM
stevea Offline
Registered User
 
Join Date: Apr 2006
Location: Ohio, USA
Posts: 8,547
linuxfirefox
Re: Concatenating files

Well tho' these abuses of cat (where is the ASPCA on this?) demonstrate a poor understanding of bash pipe redirections, they aren't harmful in non-production code. It does expose one's noob-ness but that's no fault.
__________________
None are more hopelessly enslaved than those who falsely believe they are free.
Johann Wolfgang von Goethe
Reply With Quote
  #11  
Old 1st June 2012, 05:38 PM
griffinmt Offline
Registered User
 
Join Date: Oct 2005
Location: Central Michigan
Age: 64
Posts: 190
windows_7ie
Thumbs up [CLOSED] Concatenating files

I mean NO OFFENSE to anyone on this thread, but the correct answer to the original query came on the very first reply. I just can't help thinking that all the efforts given after that point could have been better directed at solving the other hundreds (thousands?) of outstanding problems folks have posted.

Could the moderator please 'close' this thread?

And again, I really do thank all the members who take their time to help poor slobs like myself!
__________________
Martyn Griffin
Reply With Quote
  #12  
Old 1st June 2012, 07:24 PM
smr54 Online
Registered User
 
Join Date: Jan 2010
Posts: 5,389
linuxopera
Re: Concatenating files

Note that as the thread originator, you can click the thread tools and mark it solved, though this won't close it.

It's usually better to PM a mod with a request like that, as they're pretty busy (especially during the first week or so of a release) and may not see your request--you can go the main forum page, click the forums link at top, and it will then, at the bottom of that page, show you who is on line--mods are in boldface. At that point, you can, if you wish, give them a quick PM with a link to the thread, and ask them to close it.

This is the page I mean.


http://forums.fedoraforum.org/?

At this exact moment, it looks as if there's only one moderator on line.
Reply With Quote
  #13  
Old 1st June 2012, 08:28 PM
RupertPupkin Offline
Registered User
 
Join Date: Nov 2006
Location: Detroit
Posts: 5,450
linuxfedorafirefox
Re: Concatenating files

Quote:
Originally Posted by smr54 View Post
A typical misuse of cat.

cat myfile|grep testing
Yeah, that's the one that bugs me the most. C'mon, people, save the cats!

The best use of cat I've seen is for adding a line to the beginning of a file:
Code:
$ cat file.txt
This is line 2.
This is line 3.
$ echo "This is line 1." | cat - file.txt
This is line 1.
This is line 2.
This is line 3.
Reply With Quote
Reply

Tags
concatenating, files

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
livecd-creator, kickstart files, adding files to /home directory. I.E. software dedic Bastian123 Installation, Upgrades and Live Media 7 24th March 2011 11:00 AM
[Fedora 12] Restored files from USB key - source files are all binary! stodge Using Fedora 1 12th February 2010 08:56 PM
transfering files to psp results in corrupt files all the time stephanie.steve Using Fedora 6 29th September 2007 02:06 AM


Current GMT-time: 10:28 (Wednesday, 16-04-2014)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat