Advanced

Use "cat" to merge multiple files

Posted by annamese on · 7 comments
Linux and Life
Mail this story
Print this story

The "cat" command is mostly used for viewing files but not many people know that the original purpose of this command is for merging files together ("cat" is the abbreviation of "catenate", which means " to connect things together").

Full Story

  Nav
» Read more about: Story Type: Tutorial

» Get new stories by e-mail: nightly or as they are published

« Return to the newswire homepage

  Related stories
» More like this

Comments (7)

I've had tech interviews where people who should know better were floored by my unix utility approach to solving some of the problems they posed (as opposed, say, to writing a ruby/java/what have you program).

So ...

cat file1 file2 file3 | sort > sorted_file

cat file1 file2 file 3 | sort | uniq > sorted_file_no_dupes

And we haven't even tried to do any sed or awk magic.
If those files are really, really big, you can do:

cat file1 file2 file 3 | uniq | sort | uniq > sorted_file_no_dupes

to strip out the dupes before sorting.
What blew my mind was how often that works for video files.
What blew my mind is how little it takes to be published on LXer. Apparently, I can start a blog, write an entry stating:

Is The Linux Desktop Dead?

The term command to list files is ls.

Example:

$ ls /dir_name



Who knew! ;)

nb





Quoting:What blew my mind is how little it takes to be published on LXer.


Yes it's great isn't it.
@gus3 --

Careful with that...

That first uniq is only going to get the dupes that can be found without sorting. You're as likely to increase your overall execution time as to decrease it. Maybe moreso, depending on the size of the files and how sorted they are or are not.
Wait till you give an example with xargs in an interview. That floors people every time.

P.S.: xargs is your friend.

Post a comment

A free account is needed to post. Write your comment here first: your text is kept in this browser while you sign up or log in, and is waiting for you when you come back.

Your message:
|||||
10
Markdown: **bold**, *italic*, [text](https://link), - lists, > quotes, $x^2$ for math. Formatting help

Sign up and post (it is free)  or  Log in and post