Learning Linux Commands: awk--reference
http://how-to.linuxcareer.com/learning-linux-commands-awkIn this case,the title might be a little misleading. And that is because awk is more than a command,it's a programming language in its own right. You can write awk scripts for complex operations or you can use awk from the command line. The name stands for Aho,Weinberger and Kernighan (yes,Brian Kernighan),the authors of the language,which was started in 1977,hence it shares the same Unix spirit as the other classic *nix utilities. If you're getting used toor know it already,you will see some familiar concepts in awk,especially since the 'k' in awk stands for the same person as the 'k' in K&R,the C programming bible. You will need some command-line knowledge in Linux and possibly some scripting basics,but the last part is optional,as we will try to offer something for everybody. Many thanks to Arnold Robbins for all his work involved in awk. awk is a utility/language designed for data extraction. If the word "extraction" rings a bell,it should because awk was one Larry Wall's inspirations when he created Perl. awk is often used withto perform useful and practical text manipulation chores,and it depends on the task if you should use awk or Perl,but also on personal preference. Just as sed,awk reads one line at a time,performs some action depending on the condition you give it and outputs the result. One of the most simple and popular uses of awk is selecting a column from a text file or other command's output. One thing I used to do with awk was,if I installed Debian on my second workstation,to get a list of the installed software from my primary box then feed it to aptitude. For that,I did something like that: $ dpkg -l | awk ' {print $2} ' > installed
Most package managers today offer this facility,for example rpm's -qa options,but the output is more than I want. I see that the second column of dpkg -l 's output contains the name of the packages installed,so this is why I used $2 with awk: to get me only the 2nd column. As you have noticed,the action to be performed by awk is enclosed in braces,and the whole command is quoted. But the syntax is awk 'condition{action}'. In our example,we had no condition,but if we wanted to,say,check only for vim-related packages installed (yes,there is grep,but this is an example,plus why use two utilities when you can only use one?),we would have done this: $ dpkg -l | awk ' /'vim'/ {print $2} '
This command would print all packages installed that have "vim" in their names. One thing that recommend awk is that it's fast. If you replace "vim" with "lib",on my system that yields 1300 packages. There will be situations where the data you'll have to work with will be much bigger,and that's one part where awk shines. Anyway,let's start with the examples,and we will explain some concepts as we go. But before that,it would be good to know that there are several awk dialects and implementations,and the examples presented here deal with GNU awk,as an implementation and dialect. And because of various quoting issues,we assume you're using,ksh or sh,we don't support (t)csh. 相关内容
|
