AWK simple instance

xiaoxiao2021-03-06  72

Author: mlsx taken from: http: //bbs.xplore.cnAWK is a pattern scanning combined processing language, its name comes from the original author Alfred V. Aho, Perter J. Weinberger and Brian W.Kernighan acronym, the first AWK It is invented in the Bell Lab in AT & T. The AWK used under Linux is GNU AWK. AWK can be used: 1. Manage small personal database 2. Generate report 3. Generate indexes. . . . AWK is also an explanatory language, which we mainly use it to write some small tools to solve small problems in certain issues, especially for system administrators. The following is a file TEST1.DAT to illustrate some simple applications of AWK:

Code :: Cat Test1.dat One 123-321 234/22 A Two 344-637 726/28 C Three 273-287 287/97 D Four 872-872 282/20 C

The above is the contents of Test1.dat, separated by blank characters (can be space, tab, etc.) First we have to figure out several concepts: 1. Record record is the data read from the data file from the data file. Basic unit. For Test1.dat files, a record is a line. Article 1 Record: "One 123-321 234/22 A" 2. The field domain is a sub-string that is separated on the record. Taking Record "One 123-321 234/22 A" as an example of the first column. Chain, third column, Chain, One 123-321 234/22 A, a gang character, is separated from the adjacent domain. If the AWK program is paraly, then you can enter the direct input directly to: awk 'program' file 1, file 2 .. . . . For example, printing the second column of the input file

Code: awk '{print $ 2}' Test1.dat 123-321 344-637 273-287 872-872

If the program is longer, then write into a script file, execute the AWK format as follows: awk -f program file name file 1, file 2 .. . . This is different from the general shell script, and the general shell script will perform itself as a known banking file. When the file of the AWK program is more than one, the format of the AWK is executed is as follows: awk -f program file 1 -f program file 2. . . File 1, file 2 .. . . . .

Now let's take a look at the main structure of the AWK program, the main syntax in the AWK program is to represent the {action}, so the general AWK program is like this: Expressions 1 {Action 1} Expression 2 {Action 2}. . . Indicates that the formula N {action n} awk can also accept the general relationship judgment

Code ::>, <,> =, <=, ==,! = In addition, it is also acceptable ~ (in line)! ~ (Does not meet) two relational operators, such as

Code :: "xplore" ~ / or / or / this top This line is an expression because xplore contains a sub-string that can be in accordance with the / OR / OR, returns True. And the action is a number of AWK instructions. AWK's instructions in the C language is similar. For example: AWK I / O Directive: Print, Printf (), getLine ..... AWK process control instruction: if (..) {...} else {...} while (....) { ....} ..... A standard AWK program is divided into three parts: begin {}, {}, end {}, where the begin {} section is executed when the program starts, no matter whether there is input, {} How many lines are input, how much is executed; End {} is executed at the end of the program, and only once. For example, the procedure below.

Code: $ cat count.awk begin {print "now"; count = 1;} {print "this is the the" count "line"; count ;} end {print "this is the end Of file "; $ awk -f count.awk test1.dat now One said about the internal variables of AWK: 1. Domain variable IWK's domain variables and their meanings are as follows: Domain Variable Meaning $ 0 is a string, its content is the current record $ 1 representing $ 0 on the first domain of $ 0 Data $ 2 represents $ 0 data on the second domain .................... AWK also provides some other internal variables, commonly used: NF (NUMBER OF Fields) is an integer, indicating the number of domains, such as Test1.dat's number of domains is an integer, its value indicates that the number of records of the AWK intended recordings FileName is processing the data file name There are some AWK internal functions, these functions of these functions and names and Perl meet, so this will not be described in detail. In the following example, you can see. Some common examples are now mentioned. Through these examples, the AWK language will have a well-step understanding. 1. Output all the number of fields, the maximum number of fields. Code: $ awk '{if (nf> max) max = nf} end {print}' Test1.dat 42. Remove all blank lines in a file

Code :: $ awk 'nf> 0' Test1.dat One 123-321 234/22 A Two 344-637 726/28 c Three 273-287 287/97 D Four 872-872 282/20 C 3. Output a file Even line

Code: $ awk 'NR% 2 == 0' Test1.dat Two 344-637 726/28 c Four 872-872 282/20 C 4. Input range is seven random numbers from 1 to 100

Code: $ awk 'begin {for (i = 1; i <7; i ) print int (101 * rand ())}' 24 29 85 15 59 19 5. Output Number of bytes of all files in this directory

Code ::] $ ls -l | awk '{x = $ 5}; end {print x}' 15827 6. Put all usernames on the system, output according to alphabetical order

Code :: $ awk 'BEGIN {FS = ":";} {print $ 1 | "sort"}' / etc / passwd adm apache bin daemon dbus desktop ftp games gdm gopher guest halt lp mail mailnull mlsx mysql news nfsnobody nobody nscd ntp Operator PCAP PLONE ROOT RPC RPCUSER RPM Shutdown Smmsp Squid SSHD SYNC UUCP VCSA Webalizer WPM XFS 7. Statistical A total of a total bank of a file and output

Code: $ awk 'end {print "total", nr, "lines"}' Test1.dat Total 4 Lines 8. Add a line to the file and output

Code :: 1: root: x: 0: 0: root: / root: / bin / bash 2: bin: x: 1: 1: bin: / bin: / sbin / nologin 3: daem: x: 2: 2 : daemon: / sbin: / sbin / nologin 4: ADM: x: 3: 4: ADM: / var / adm: / sbin / nologin 5: lp: x: 4: 7: lp: / var / spool / lpd: / sbin / nologin 6: sync: x: 5: 0: Sync: / sbin: / bin / sync 7: shutdown: x: 6: 0: Shutdown: / sbin: / sbin / shutdown 8: Halt: x: 7: 0: Halt: / sbin: / sbin / halt 9: mail: x: 8: 12: mail: / var / spool / mail: / sbin / nologin 10: News: x: 9: 13: News: / etc / news : 11: UUCP: X: 10: 14: UUCP: / VAR / Spool / UUCP: / SBIN / NOLOGIN 12: Operator: x: 11: 0: Operator: / root: / sbin / nologin 13: Games: x: 12 : 100: Games: / usr / games: / sbin / nologin 14: Gopher: x: 13: 30: Gopher: / var / gopher: / sbin / nologin 15: ftp: x: 14: 50: FTP User: / var / ftp: / sbin / nologin 16: Nobody: x: 99: 99: NOBODY: /: / SBIN / NOLOGIN 17: RPM: x: 37: 37 :: / var / lib / rpm: / bin / bash 18: VCSA : x: 69: 69: Virtual console memory Owner: / dev: / sbin / nologin 19: NSCD: x: 28: 28: NSCD daem: /: / sbin / nologin 20: sshd: x: 74: 74: privilege- Separated ssh: / var / empty / sshd: / sbin / nologin 21: RPC: x: 32: Portmapper rpc user: /: / sbin / nologin 22: RPCUser: x: 29: 29: RPC Service User: / var / lib / nfs: / sbin / nologin 23: NFSNOBODY: x: 65534: 65534: Anonymous NFS User: / VAR / lib / nfs: / sbin / nologin 24: Mailnull: x: 47: 47 :: / Var / spool / mqueue: / sbin / nologin 25: SMMSP: x: 51: 51 :: / var / spool / mqueue: / sbin / nologin 26: PCAP: x: 77: 77 :: / var / ardwatch: / sbin / Nologin 27: NTP: x: 38: 38 :: / ETC / NTP: / SBIN / NOLOGIN 28: GDM: X: 42: 42 :: / VAR / GDM: / SBIN / NOLOGIN 29: Desktop: x: 80: 80: Desktop: / var / lib / menu / kde: / sbin / NOLOGIN 30: MLSX: x: 500: 48: Weiguozhao: / home / mlsx: / bin / bash 31: apache: x: 48: 0: Apache: / VAR / WWW: / SBIN / NOLOGIN 32: Webalizer: x: 67: 67: Webalizer: / VAR / WWW / HTML / USAGE: / SBIN / NOLOGIN 33: Guest: x: 501: 501 :: / Home / Guest: / BIN / BASH 34: WPM: x: 502: 502 :: / Home / WPM: / Bash 35: DBUS: x: 81: 81: System Message Bus: /: / sbin / nologin 36: plone: ​​x: 101: 102: Plone User: / var / lib / plone2 / main:

/ bin / false 37: Squid: x: 23: 23 :: / var / spool / squid: / sbin / nologin 38: xfs: x: 43: 43: x font server: / etc / x11 / fs: / sbin / Nologin 39: MySQL: X: 103: 104: MySQL Server: / var / lib / mysql: / bin / bash 9. Output file included in 'One' Row Code: $ awk '/ one / {print $ 0}' TEST1.DAT One 123-321 234/22 A 10. List of mailboxes for regular users on the system (assuming domain names are cie.xtu.edu.cn)

Code: $ awk 'begin {fs = ":"} {= ":"} {IF ($ 3> 499) Print $ 1 "@ cie.xtu.edu.cn"}' / etc / passwd nfsnobody@cie.xtu.edu.cn mlsx @ cie.xtu.edu.cn guest@cie.xtu.edu.cn wpm@cie.xtu.edu.cn

转载请注明原文地址:https://www.9cbs.com/read-92237.html

New Post(0)