Unix: the power of simple tools.

Story: Use "cat" to merge multiple filesTotal Replies: 6
Author Content

Jul 08, 2012
4:00 PM EDT
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.

Jul 08, 2012
6:08 PM EDT
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.

Jul 08, 2012
6:51 PM EDT
What blew my mind was how often that works for video files.

Jul 08, 2012
7:02 PM EDT
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.


$ ls /dir_name

Who knew! ;)



Jul 08, 2012
7:07 PM EDT
Quoting:What blew my mind is how little it takes to be published on LXer.

Yes it's great isn't it.

Jul 08, 2012
7:45 PM EDT
@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.

Jul 09, 2012
11:06 PM EDT
Wait till you give an example with xargs in an interview. That floors people every time.

P.S.: xargs is your friend.

Posting in this forum is limited to members of the group: [ForumMods, SITEADMINS, MEMBERS.]

Becoming a member of LXer is easy and free. Join Us!