Good Ol' sed

 in
A nice little command to help you modify files.

When I started using UNIX in 1982, life in the computer world was fairly cruel. At that time, most programmers were still using line editors. The UNIX line editor, ed, was a relief in comparison with most line editors of other operating systems. Its sensible use of regular expressions was a blessing, and the fun part was that most UNIX tools used the same kind of regular expressions.

Although UNIX had virtual memory, the size of files that ed could handle was limited; disk space for memory was expensive. For large files, programmers had to resort to the stream editor, sed. sed reads its input line-after-line and performs its editing operations line-by-line. In sed, some commands allow use of multi-lines and so have a holding space, but in general, the amount of memory needed is small.

Besides the occasional one-line commands, I often wrote sed scripts. In those days, most system administration scripts were written in sed; awk was too slow and too big. The power demonstrated by those sed scripts was and still is quite amazing. They were true works of art—large and completely incomprehensible, but they got the job done.

Since sed is Turing-complete, it is as powerful as any programming language. Writing sed scripts that compute certain functions became a sport. Olaf Kirch, author of the Linux Network Administrator's Guide, says in his preface that he was proud to have written a prime number generator in sed. My pet script computes the Ackermann function and is available for anonymous download in the file ftp://ftp.linuxjournal.com/pub/lj/listings/issue60/2628.tgz along with a short explanation. It is just like programming in assembler.

Today, sed scripts are totally different—they are much simpler and are almost always one-liners. Most one-line sed commands (often included in Bourne scripts or used interactively in your shell of choice) modify or delete certain lines in a file. In some cases, you might still write sed scripts; however, the commands remain simple. Beside the two operations just mentioned, you also insert, append and change groups of lines as a block.

The advanced sed commands have disappeared (and I must say I'm glad). Although these advanced commands made sed powerful, they also made the scripts unreadable. Today, if you need to do something advanced, you would use awk or Perl.

Syntax

I will not describe every feature of sed. Instead, I will restrict myself to just those commands I regularly use. For more information on sed, the best resource is sed & awk by Dale Dougherty and Arnold Robbins (O'Reilly & Associates, 1997).

sed commands have the following form with no trailing spaces:

[address][,address][!]command[arguments]

I will begin with the address. An address is either a line number ($ for the last line) or a regular expression enclosed in slashes. The regular expressions are similar to the ones you see in vi (well, actually the ex part of vi): “.” (any character), “*” (any number of the immediately preceding regular expression), “[class]” (any character in class), “[^class]” (any character not in class), “^” (begining of line), “$” (end of line) and ''\'' (to escape characters where needed).

A range of lines can be specified by giving two addresses. The “!” after the address specification excludes that range from being processed. The most commonly used sed commands are “d” (delete) and “s” (substitute). The delete command is straightforward; it deletes any line that matches the entire address specification. Substitute is more interesting:

s/pattern/replacement/[g]

Basically, pattern is just a regular expression, but it has an odd feature: parts of the pattern can be stored in the replacement. The parts to keep must be enclosed within the characters “\(” and “\)”. In the replacement part, these stored parts can be used by specifying “\1”, “\2”, ..., “\9” (the first, the second, ..., and the ninth stored part). If the entire matched part is to be used, the “&” character is specified. The g (global) flag can be used to replace all occurrences of pattern by replacement.

______________________

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState