Command and Conquer - Output Formmating

Author: Full Circle Magazine. Link to original: http://fullcirclemagazine.org/ (English).
Tags: Full_Circle_Magazine, Ubuntu Submitted by slavic 05.02.2009. Public material.

Translations of this material:

into Russian: Command and Conquer - Output Formmating. Translation complete.
Submitted for translation by slavic 05.02.2009 Published 3 years, 3 months ago.

Text

In this instalment of Command & Conquer, we will cover the basic uses of grep, sed, awk, cat, and cut for formatting output. This can be useful when putting together things such as Conky, or scripts that display theme information in the terminal.

The first command we should look at is cut. If, for example, we wanted to display the distribution name in a theme script, we would find it in /etc/issue. If, however, we run cat /etc/issue we see that there is one line too many, and there are escape characters included in the line. So if we run /etc/issue|head -n 1, we remove the extra line by piping the output of cat through head, which then displays only the first line of the output. So far so good, but what about the escape characters? This is where cut comes in handy. To use cut, we must supply a delimiter, and then tell it what to do with this. The command we would use is:

This command then tells cut that the delimiter to use is a blank space, and to display the first two fields (basically, cut slices output up into segments according to the delimiter, so fields 1 and 2 are the first fields before and after the first delimiter in the output, in our case, Ubuntu 8.10). Cut can also be used to display only a certain number of characters when using the -c flag.

With sed the same could be done with:

This may look like gibberish, but the first two expressions (each expression is separated by a semi-colon) tell sed to substitute “\n” with “” (nothing), and the same for “\l”, removing those characters from the output. “/^$/d” is a command that tells sed to delete any blank lines (“^$” is the regular expression for a line that begins with a blank and ends with a blank and has nothing between those -- a blank line). So 's/\\n\b//' is merely telling sed to substitute (“s/”) “\n” (“\\n”) with “” (“//”). The reason the command is in braces is because we are actually applying three expressions on the output, and want it returned only once, so we put the expressions in braces (“{}”), and separated by semi-colons.

Lastly, the same output can be achieved using awk:

This command again uses regular expressions, but is slightly easier to understand than sed. Basically, awk '/\n/ {print $1, $2}' finds any line that has “\n” in it, then prints the first two fields (the default separator is a space, but you can set your own using the -F flag).

This saves us having to format out the extra line and the \l of the output. You could also forgo piping the output of cat /etc/issue into the command (or either of the others), as they can all be applied to a file specified at the end of the command. I used cat in order to leave the commands less jumbled.

This is intended only as an introductory look at the abilities of awk, sed, and cut. Their flexible implementations make it hard to write a brief in-depth tutorial for the three of them. The above explanations are intended to illustrate how the commands work, and not fully explore their potential uses. A real-world implementation of these commands would be in the first half of a custom theme script (the example below also displays theme info, but that part isn't necessarily pertinent to this article; it was left there to keep the script complete).

The example also contains a challenge for anyone who wishes to attempt it: Figure out how to use one of the three commands to remove the indentation in the memory part of the script, and, if you want more practice, try replacing every occurrence of cut, sed, or awk with a different command that does the same (i.e. replace a cut command with awk). There is no prize, but it is good practice to figure out the inner workings of the commands.

License: Creative Commons Attribution-Share Alike 3.0 Unported License