Good Ol' sed
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.
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:
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:
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.
Fast/Flexible Linux OS Recovery
On Demand Now
In this live one-hour webinar, learn how to enhance your existing backup strategies for complete disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible full-system recovery solution for UNIX and Linux systems.
Join Linux Journal's Shawn Powers and David Huffman, President/CEO, Storix, Inc.
Free to Linux Journal readers.Register Now!
- Server Hardening
- BitTorrent Inc.'s Sync
- Download "Linux Management with Red Hat Satellite: Measuring Business Impact and ROI"
- New Container Image Standard Promises More Portable Apps
- The Humble Hacker?
- The Death of RoboVM
- The US Government and Open-Source Software
- Open-Source Project Secretly Funded by CIA
- EnterpriseDB's EDB Postgres Advanced Server and EDB Postgres Enterprise Manager
- AdaCore's SPARK Pro
In modern computer systems, privacy and security are mandatory. However, connections from the outside over public networks automatically imply risks. One easily available solution to avoid eavesdroppers’ attempts is SSH. But, its wide adoption during the past 21 years has made it a target for attackers, so hardening your system properly is a must.
Additionally, in highly regulated markets, you must comply with specific operational requirements, proving that you conform to standards and even that you have included new mandatory authentication methods, such as two-factor authentication. In this ebook, I discuss SSH and how to configure and manage it to guarantee that your network is safe, your data is secure and that you comply with relevant regulations.Get the Guide