The full characteristics of A W K are not described below, nor involve a deep-level programming of A W K, only describe the use of A W K execution row operation and how to extract information from text files and strings.
Quote: The content is: • Extract domain. • Match regular expressions. • The comparison domain. • Pass parameters to A W k. • Basic A W K row operations and scripts.
The most basic feature of a W k language is to browse and extract information based on the specified rules in the file or string. A W k will be operated after the information is extracted. Complete A W K scripts are usually used to format information in text files. 1 Call awk There are three ways to call A W K, the first is the command line mode, such as:
Code: awk [-f fild-separator] 'Commands' Input-file (s)
Here, C O m M m a n d is a true A W k command. In the above example, [- F Domain Division] is optional because the AWK uses spaces as the default domain separator, so if you want to browse the text between the domain, you don't have to specify this option, but if you want to browse, if you want to browse, such as Passwd Document, this file varies as a separator in each domain, you must specify the - f option, such as:
Code: awk -f: 'Commands' Input-File (s)
The second method is to insert all A w k commands into a file and make the A W k program executable, then use the A W k command interpreter as the first row of the script to call it by typing the scrip name. The third way is to insert all A w k command into a separate file, then call:
Code: awk -f awk-script-file input-files (s)
- f Option indicates that the A W K script in file a w k _ s c r i p_ f i l e, I N p U t _ f i l e (s) is a file name that browsing using A W K. 2 AWK scripts When calling A w k in the command, the A Wk script consists of various operations and modes. If the -f option is set, AWK reads a record or row each time, and divides the specified domain using the specified separator, but if the -f option is not set, the awk assumes that the space is domain separator, and keeps this setting until it is found New line. When the new row appears, the A W k command learns that the full strip record is read, then start the read command in the next record, this read process will continue until the file end or file no longer exists. Refer to the table, AWK reads a line in the file, find the domain separator (here is symbol #), set it as domain N until a new line (here is the default record separator), then divide this line as a Record, then the AWK will start the next line of reading again. AWK reading file record
Quote: Domain 1 Split Downtown 2 Split Field 3 Split Field 4 and Renewal P. B Unny (Record 1) # 0 2/9 9 # 4 8 # Yellow / N J. Tr Oll (Record 2) # 0 7 / 9 9 # 4 8 4 2 # brown-3 / n
2.1 Mode and action Any A W k statement consists of mode and action. There may be many statements in a A W k script. The mode portion determines when the action statement triggers and triggers event. Processing is the operation of the data. If the mode portion is omitted, the action will remain performing the state. The mode can be any conditional statement or a composite statement or a regular expression. Mode includes two special fields B E g i n and e n D. Use the B E G i n statement setting counts and printheads. B E g i n statement Use the text browsing action before the input file is started based on the input file before any text browsing action. E N D statement is used to print the total number of print output text after the text browsing action in A W k, and the end status flag. If the mode is not specified, A W K always matches or prints the number of lines. The actual action is indicated in the braces {}. Most of the actions are used to print, but there are still some longer code such as I F and cyclic (L OE P i N g) statements and cyclic exit structures. If you do not specify actions, A W k will print out all the records that browse. 2. When the domain and record A W k are executed, its browsing field is marked as $ 1, $ 2... This method is called domain identification. Use these domain identities will be further processed more easily. Use $ 1, $ 3 to refer to 1th and 3th domain, note that the domain is separated by a comma. If you want to print a domain with 5 logs, you don't have to specify $ 1, $ 2, $ 3, $ 4, you can use $ 0, and all domains. When AW k, reach a new line, that is, assume that the end of the record containing the domain, then perform the read action of the new record, and reset the domain separation. Be careful not to confuse the symbols $ and S H E L L prompt $, they are different. To print a domain or all domains, use the P R I n t command. This is an A Wk action (the motion syntax is enclosed in parentheses). 1. The extraction domain really performs a few examples, and the existing text file G R A D e. T X t records a row information called the judo database.
Code: $ cat grade.txt m.tans 5/99 48311 Green 8 40 44 J.LULU 06/99 48317 Green 9 24 26 P.bunny 02/99 48 Yellow 12 35 28 J. Troll 07/99 4842 Brown-3 12 26 26 L.Tansl 05/99 4712 Brown-2 12 30 28 This text file has seven domains, ie (1) name, (2) Date of the segment, (3) Student serial number, (4) Belt level, 5) Age, (6) The current competition points, (7) the highest score. Because the domain is used as a domain separator, it is not necessary to use the -f option to divide the domain, now browse the file and export some data. In the example, in order to facilitate display, the space is widened to make the various domains can be clearer. 2. Save A W K Output There are two ways to save the output of the A W k script under the S H E L L prompt. The simplest way is to use the output redirection symbol> file name, the following example redirect output to file w o w.
Code: $ awk '{print $ 0}' grade.txt> WOW $ cat grade.txt Use this method to note that the output is not displayed on the display. Because it outputs it directly to the file. This method is only used when the result is guaranteed. It also overrides the same name data on the hard disk. The second method is to use the T E E command to output to the screen while outputting to the file. This method is used most when the results of the test output is correct. For example, output redirects to file D e l e t e _ m e _ a n D _ D i E, output to the screen. Use this method to write in the end of the A W k command | Tee delete_me_and_die. Code: $ awk '{print $ 0}' grade.txt | Tee delete_me_and_die3. Using standard input before explaining this chapter, first introduce the input method of the A Wk script. In fact any script is accepted from standard input. To run this script, use a Wk script to enter file format, for example:
Quote: BELTS.AWK grade_student.txt can also replace the following format: Belts.awk Code: $ awk '{print $ 0}' grade.txta w k Read each record. Because there is no mode part, only the action section {Print $ 0} (printing all records), this action must be enclosed with a parentheses. The above command prints the entire file. 5. Print Separate Record Assume that only the student name and belt level are printed, by viewing the domain, you can know that F i E L D - 1 and F I E L D - 4, so $ 1 and $ 4 can be used, but don't forget the Gag with the granaries. Code: $ awk '{Print $ 1, $ 4}' grade.txt m.tans Green J.LULU Green P.bunny Yellow J. Troll Brown-3 L.Tansl Brown-26. Print report header The above command output in the name and belt There are some spaces between levels to make it easier to divide, or it can be divided into the domain. To add a T A B key, use the T A B key speedscript reference / T, and will be discussed in detail later. You can also add information heads for output text. In this example, N A M E and B E L T and Underline are added. Underline use / n, forced to start the new row, and launch the print text operation in / n next line. The print information head is placed in the B E G I n mode part because the print information head is defined as an action, and must be enclosed in braces. In A W k, the information head is printed before the first record. Code: $ awk 'begin {print "name Belt / N ---------------------------------"} {Print $ 1 "/ T", $ 4} 'grade.txt name Belt -------------------------------- - M.Tans Green J.LULU GREEN P.BUNNY YELLOW J.TROLL Brown-3 L.Tansl Brown-27. The print information is available if End of Report information is added to the end. The e n d statement is executed after all text processing movements are executed. E N D statement is placed behind the main action in the position of the script. The following is simple printhead information and the query action is completed. Code: $ awk 'begin {print "name / n --------"} {print $ 1} end {"end-of-report"}' grade.txt name ------ M .Tans j.lulu p.bunny j.troll l.tansl8. AWK error message prompts almost certainly, when using awk, some errors will be encountered in the command. A W k will try to print an error, but because most of the commands are only in one line, it is not good. The display error message given by the system prompts readability. Using the above example, if you lose a double quotation mark, AWK will return: Code: $ awk 'begin {print "name / n --------} {Print $ 1} End {" end-of-report "} 'grade.txt awk: cmd. Line: 1: begin {print "name / n --------} {Print $ 1} end {" end-of-}} awk: cmd. line: 1: ^ Unterminated String When you use AWK for the first time, it may be impressed by the error message, but through long and constant learning, the following rules can be summarized. When you touch a W k error, you can find it according to: Quote: • Make sure the entire A W k command is enclosed in single quotes. • Make sure all quotes in the command appear to appear. • Make sure that the action statement is enclosed with a parentheses, and the conditional statement is enclosed in parentheses. • May forget to use the curly brackets, maybe you think there is no need, but A w k does not think so will explain the syntax. If the query file does not exist, you will get the following error message: Code: $ awk 'end {print nr}' gradees.txt awk: cmd. Line: 2: Fatal: Cannot Open file `gradees.txt 'forreading (no file or directory) 9.awk keyboard Enter If you are in the command line There is no input file grade. TXT, what will it be? Code: $ awk 'begin {print "name / n --------"} {print $ 1} end {"end-of-report"}' name ------- Begin section Print The file header, but the awk finally stops the operation and waits, and has not returned the shell prompt. This is because A Wk expects to get the keyboard input. Because no input file is given, A Wk assumes that the following will be given. If you prefer, enter the relevant text in order, and knock the Code: / ^ $. [] | () * ? There are two characters in Chapter 7, because they only apply to A W K without applicable to G R E P or S E d. They are: Quote: Use match one or more characters. ? The matching mode has a frequency. For example, using / x y? Z / match x y z or y z.