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.
Practical Task Scheduling Deployment
July 20, 2016 12:00 pm CDT
One of the best things about the UNIX environment (aside from being stable and efficient) is the vast array of software tools available to help you do your job. Traditionally, a UNIX tool does only one thing, but does that one thing very well. For example, grep is very easy to use and can search vast amounts of data quickly. The find tool can find a particular file or files based on all kinds of criteria. It's pretty easy to string these tools together to build even more powerful tools, such as a tool that finds all of the .log files in the /home directory and searches each one for a particular entry. This erector-set mentality allows UNIX system administrators to seem to always have the right tool for the job.
Cron traditionally has been considered another such a tool for job scheduling, but is it enough? This webinar considers that very question. The first part builds on a previous Geek Guide, Beyond Cron, and briefly describes how to know when it might be time to consider upgrading your job scheduling infrastructure. The second part presents an actual planning and implementation framework.
Join Linux Journal's Mike Diehl and Pat Cameron of Help Systems.
Free to Linux Journal readers.Register Now!
- SUSE LLC's SUSE Manager
- Murat Yener and Onur Dundar's Expert Android Studio (Wrox)
- My +1 Sword of Productivity
- Managing Linux Using Puppet
- Non-Linux FOSS: Caffeine!
- Doing for User Space What We Did for Kernel Space
- SuperTuxKart 0.9.2 Released
- Google's SwiftShader Released
- Parsing an RSS News Feed with a Bash Script
- Rogue Wave Software's Zend Server
With all the industry talk about the benefits of Linux on Power and all the performance advantages offered by its open architecture, you may be considering a move in that direction. If you are thinking about analytics, big data and cloud computing, you would be right to evaluate Power. The idea of using commodity x86 hardware and replacing it every three years is an outdated cost model. It doesn’t consider the total cost of ownership, and it doesn’t consider the advantage of real processing power, high-availability and multithreading like a demon.
This ebook takes a look at some of the practical applications of the Linux on Power platform and ways you might bring all the performance power of this open architecture to bear for your organization. There are no smoke and mirrors here—just hard, cold, empirical evidence provided by independent sources. I also consider some innovative ways Linux on Power will be used in the future.Get the Guide