Daily Shaarli

All links of one day in a single page.

March 23, 2013

PHP Caching to Speed up Dynamically Generated Sites

This entire site, like many, is built in PHP. PHP provides the power to simply 'pull' content from an external source, in the case of my site this is flat files but it could just as easily be an MySQL database or an XML file etc..

The downside to this is processing time, each request for one page can trigger multiple database queries, processing of the output, and formatting it for display... This can be quite slow on complex sites (or slower servers)

Ironically, these so-called 'dynamic' sites probably have very little changing content, this page will almost never be updated after the day it is written - yet each time someone requests it the scripts goes and fetches the content, applies various functions and filters to it, then outputs it to you...

Understanding Awk in the UNIX Shell

So what is awk, and what does it have to do with UNIX? Plenty, as you'll soon learn. It is a tool that helps you get certain tasks done in the UNIX shell. But it's no mere tool; it's so developed that it's a programming language in its own right. Keep reading to learn how to make the most of awk.
The core idea in the creation of any programming/scripting language is to make it as natural and as simple as possible. Still, it should allow the construction of advanced expressions for solving complex problems. The creators of the UNIX Shell script language did not forgot these two simple principles. For the sake of simplicity some parts are broken down into multiple sub-parts. Sometimes these sub-parts evolve and grow to form their own programming language.

A prime example for this is Awk. The name "awk" comes from the starting letters of the names of the three creators: Aho - Weinberg - Kernighan. These people defined it as "awk, a pattern scanning and processing language." Besides setting a concrete purpose for the tool, this also underlines the fact that awk has its own syntax and rules. With this, it becomes a programming language on its own.

Awk was designed to scan and process files like the .cvs, where data are organized into columns and rows. However, doing the same with any other source of organized data in this structure is a valid option (as in the case of the command ls -l). The principle behind awk is to divide an input stream into rows and records and make the changes on this.

Last time I presented the stream editor, which accomplished the row splitting. However, compared to the sed, awk is a much more complex, powerful and more capable language. The record extraction allows us to throw away the unnecessary and process only the useful information from a file/UNIX tool.

Awk is a scripting language, and is best for solving small everyday problems. Its three creators do not recommend it for use in big, complex problem solving. However, there are long lists of problems that can be solved with it. It also has a couple of other advantages when compared to other tools, like the sed. For instance, it can work with real numbers and follow a very C-like syntax.

In this and a future article I will try to present it as concisely as I can without leaving out any crucial parts of awk. Remember that this article is only designed to introduce to you this language on a basic level. I do not intend to show you every corner of the scripting language. Nevertheless, this will be enough for you to use it in the everyday problems you may come across in the future.

Stream Editor in the UNIX Shell

Making a change sometimes takes a great effort coupled with a great sacrifice. At other times, though, it simply calls for a little adjustment of what we already have. The stream editor does just this. Since inside the UNIX shell, the universal language spoken is the text stream, with the help of this tool you can make some easy and fast modifications to the shell. If you are interested in what you can change and how you can change it with the stream editor, you will have to read this article.
This article is part of my series related to shell programming in UNIX. Therefore, I will build on the fact that you can already write a script and run it inside a terminal. In addition, you should also know how to handle the control mechanism of the UNIX shell. All of these things are explained in my previous articles, so if by chance you missed them just search them up and embrace their knowledge.