Twitter Updates

12/3/2005

Sed - An Introduction and Tutorial

Filed under: — AlienMind @ 12:29 pm

Sed (or “sed”, the UNIX stream editor) can be used to edit things as they go through a pipe.

In UNIX parlance, that is one program that feeds it’s output directly to another program. It’s common for UNIX programs to do one thing, and (hopefully) do it well.

An example:

cat mywords.txt | sort | uniq | wc -l

The above example sends the file “mywords.txt” to the sort utility (which sorts the words in alphabetical order), then through “uniq” (which only leaves one instance of any word) and then to the “wc” utility which does a word count. This gives me a unique word count (in a case sensitive way), and uses pipes to communicate between the programs.

Well, if I had done this:

cat mywords.txt | sort | uniq | sed 's/A/x/g'

the last step would have, after sorting and uniqing, replaced all capital “A” characters with a lower case “x”. A simple – and useless – example, but it does illustrate what sed is for.

On digg I came across a link to this Sed introduction and tutorial. Useful if this sort of thing interests you.

  22 queries. 2.992 seconds. Powered by WordPress