Archive for August, 2010

Words filtering with simple unix commands

August 25, 2010 Leave a comment

Let’s say I have a dictionary file named allwords containing sorted words with one word per line and I also have a collection of “offensive” words in a file call badwords (also one word per line but not sorted). Now I want to take out all the offensive words listed in badwords from the original dictionary file. With Unix this task becomes a piece of cake

$ sort -u badwords > badwords.sort
$ comm -23 allwords badwords.sort > goodwords

The first command can be omitted if badwords is already sorted. In order to get the result as expected, both files need to have the same line terminators (which can be found out easily with command file). There are a number of ways to convert CRLF terminated text to *nix format, here’s my favorite:

$ perl -pi -e 's/\r\n/\n/g' input.file
[ credit goes to ]

Categories: linux, Tip