个人工具

FC21命令行

来自Ubuntu中文

跳转至: 导航, 搜索

In this installment 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 only displays 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:

cat /etc/issue|head -n 1|cut –delimiter=” “ -f 1,2

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:

cat /etc/issue|sed '{s/\\n// ; s/\\l// ; /^$/d}'

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:

cat /etc/issue|awk '/\\n/ {print $1,$2}'

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.

LINK TO THE SCRIPT WILL GO HERE

Further Reading: Sed - http://www.grymoire.com/Unix/Sed.html awk - http://www.linuxjournal.com/article/8913 or http://www.linuxfocus.org/English/September1999/article103.html cut - http://learnlinux.tsf.org.za/courses/build/shell-scripting/ch03s04.html Also, the manpage of each command (accessed with man [command]) is very useful if you're unsure of how to invoke it.

Bio: Lucas is a , 17 years old Canadian/German Linux enthusiast. He also enjoys spending time with his family, his girlfriend, and when he's not with either, expanding his knowledge of computer systems (be they Mac, Unix, Linux or Windows). He lives in Germany and has learned all he knows from repeatedly breaking his system, then having no other option but to discover how to fix it. When he finds time, he also publishes a blog at http://lswest-ubuntu.blogspot.com.

参看