KBD

Keith Devens .com

Saturday, August 30, 2008 Flag waving
"It's no trick for talented people to be interesting, but it's a gift to be interested. We want... – Randy S. Nelson (dean of Pixar University)

Archive: January 17, 2006

← January 16, 2006January 18, 2006 →

Daily link icon Tuesday, January 17, 2006

Unix command line scripting question

I'm wondering if it's possible to do the following just using unix command line utilities. Given a text file with duplicate lines, give a printout of all unique lines in the file and a count of how many copies there are of each.

Example file:

foo
bar
foo
baz
bar

Example output:

foo    2
bar    2
baz    1

It'd be trivial for me to write a script to accomplish this. I'm just wondering if there's a simple way to do it with sort, uniq, and awk or whatever.

Update: Solutions in the comments (thanks!).

Also, that should probably be "bar 2, foo 2, baz 1" for that last example, since "bar" comes before "foo" alphabetically. Though, ironically the order I gave is what you get with the straightforward sort -rn.

  1. Adam Langley on Factor:

    Factor - a programming language; very Forth like. I'm playing with it when I get a chance. I'm still not sure about stack based languages. They have nice advantages: factoring out functions is stupidly easy and when the data flow works, it's very elegant. But one cannot understand only part of a function (which is why each function must be small) and, when dealing with > 3 variables, the stack fuzz is crazy. None the less, factor has a good environment (based on jEdit, by the same author) and Erlang like concurrency.

    I've had a lot of interest in Factor lately and have been following its development. I don't have any experience in stack languages, so I want to learn its way of thinking.

       (3) Tags: [Programming]
← January 16, 2006January 18, 2006 →
August 2008
SunMonTueWedThuFriSat
 12
3456789
10111213141516
17181920212223
24252627282930
31 



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 2 posts

Recent comments XML

new⇒Johnny Walker Blue Label

Wow, thanks for the scotch review​:D

Lagavulin and Laphroaig are​some of...

Keith: Aug 29, 3:35pm

Girls, please don't get breast implants

Wow, After all this time, the​comments on this page continue to​grow. It wa...

Ajeet: Aug 25, 2:36am

Generated in about 0.043s.

(Used 7 db queries)

mobile phone