加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

Learning Linux Commands: awk--reference

发布时间:2020-05-24 08:09:42 所属栏目:Linux 来源:互联网
导读:http://how-to.linuxcareer.com/learning-linux-commands-awk1.IntroductionIn this case, the title might

http://how-to.linuxcareer.com/learning-linux-commands-awk

In 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.

awk ' {print $1,$3} ' awk ' {print $0} ' awk ' /'pattern'/ {print $2} ' awk -f script.awk inputfile awk ' program ' inputfile awk "BEGIN { print "Hello,world!!" }" awk '{ print }' #! /bin/awk -fBEGIN { print "Hello,world!" } # This is a program that prints "Hello,world!"# and exits awk -F "" 'program' files awk -F "regex" 'program' files awk 'BEGIN { print "Here is a single quote <'''>" }' . Here's why we prefer Bourne shells. :)awk '{ if (length($0) > max) max = length($0) }END { print max }' inputfile awk 'length($0) > 80' inputfile awk 'NF > 0' data awk 'BEGIN { for (i = 1; i <= 7; i++)print int(101 * rand()) }' ls -l . | awk '{ x += $5 } ; END { print "total bytes: " x }'total bytes: 7449362 ls -l . | awk '{ x += $5 } ; END { print "total kilobytes: " (x + 1023)/1024 }'total kilobytes: 7275.85 awk -F: '{ print $1 }' /etc/passwd | sort awk 'END { print NR }' inputfile awk 'NR % 2 == 0' data </tr>
<tr>
<td>

ls -l | awk '$6 == "Nov" { sum += $5 }END { print sum }'
awk '$1 /J/' inputfile awk '$1 !/J/' inputfile awk 'BEGIN { print "He said "hi!" to her." }' echo aaaabcd | awk '{ sub(/a+/,""); print }' ls -lh | awk '{ owner = $3 ; $3 = $3 " 0wnz"; print $3 }' | uniq awk '{ $2 = $2 - 10; print $0 }' inventory awk '{ $6 = ($5 + $4 + $3 + $2); print $6' inventory echo a b c d | awk '{ OFS = ":"; $2 = ""> print $0; print NF }' echo a b c d | awk ’{ OFS = ":"; $2 = ""; $6 = "new"> print $0; print NF }’ echo a b c d e f | awk ’{ print "NF =",NF;> NF = 3; print $0 }’ FS=[ ] echo ' a b c d ' | awk 'BEGIN { FS = "[ tn]+" }> { print $2 }' awk -n '/RE/{p;q;}' file.txt awk -F\ ’...’ inputfiles ... BEGIN { RS = "" ; FS = "n" }{print "Name is:",$1print "Address is:",$2print "City and State are:",$3print ""}

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!