AWK This section describes how to complete these tasks if you want to format messages or extract packets from a large text file. It is excellent in performance in text browsing and data. Overall, A w k is the most difficult to master in all S h e l L filter, do not know why, maybe it is its complex syntax or meaningful error message. In learning a W k language, you will slowly grasp erroneous information such as Bailing Out and A W K: C M d. L i n E:. It can be said that A w k is a self-explanatory programming language, which is to use A w k in S H E L1 because A W k itself is a good example of learning, but combined with A W k, such as G R E P and S E d, will make S H E L L programming easier. This chapter does not describe all of the characteristics of A W K, nor involving a deep programming of A W K, (these can be found in books of A W K). This chapter focuses on telling the use of A W K to perform row operations and how to extract information from text files and strings. This chapter is: • Extract the domain. • Match regular expressions. • The comparison domain. • Pass parameters to A W k. • Basic A W K row operations and scripts. Almost all scripts containing a W k command combine SE D and G R E P to extract information from text files and strings. In order to obtain the required information, the text must be formatted, the meaning of the domain separator division, the separator may be arbitrary characters, and will be discussed in detail when A w k will be described later. A W k is named with human A H o. WE N i N b E RG E R and K E R N I g H A m named this language. There are N a w k and g a w k, which expanded the textual characteristics, but this chapter will not discuss it. 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. 9.1 Call awk There are three ways to call A w k, the first is the command line mode, such as: Here, C O m M a n d is a true A W k command. This chapter will often use this method. 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: the second method is to insert all the awk commands into a file and make the awk program executable, then use the awk command interpreter as a script The first line in order to call it by typing the script name. The third way is to insert all A w k command into a separate file, then call: - f Option indicates 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 with A W K. 9.2 AWK Scripts When you call 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 Table 9 - 1, AWK reads a line in the file, find the domain separator (here is symbol #), set it as domain N until a new row (here is the default record separator), then divide this One line as a record, then the AWK will start the next line of reading again. Table 9-1 AWK Read File Record Method Domain 1 Split Field 2 Split Field 3 Split Domain 4 and Renewal P. B Unny (Record 1) # 0 2/9 # 4 8 # Yellow / N J. Tr Oll (Record 2) # 0 7/9 9 # 4 8 4 2 # brown-3 / n 9.2.1 Mode and Action Any awk statement consists of mode and action. There may be many statements in a A W k script. The mode part determines when the statement is triggered and triggered. Processing is the operation of the data. If the mode portion is omitted, the action will keep the execution 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 complete the total number of printouts and end status flags after the text browsing operation in A W k. 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. These patterns and actions will be explained in depth. 9.2.2 Domains and Record A W K Execution, its browsing field is marked as $ 1, $ 2... This method is called domain identification. Use these domain identities will be more easily processed. 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). Chapter 9 AWK Introduction 67 Download 1. Extractive Domain Real Execution See Several examples, the existing text file G R A D e. T X t, record a row of row of judo databases. This text file has seven domains, namely (1) name, (2) a segment date, (3) student serial number, (4) Belt level, (5) age, (6) Currently competition points, (7) the highest competition Minute. 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. Use this method to note that the output result is not displayed on the display. Because it outputs it directly to the file. This method is only used when the result is correct. 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. 3. Use standard inputs 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 book script, use a Wk script to enter file format, such as: Alternative to use the following format: Use a redirection method: or pipeline method: 4. Print all records A W K reads each record. Because there is no mode part, only the action part {Print $ 0} (printing all records), this action must be enclosed with a parentheses. The above command prints the entire file. 68 Part 2 Text Filter Download 5. Print Separate Record Assume that only the student name and belt level are printed, by viewing the domain, you can know that Field - 1 and Field - 4, so $ 1 and $ 4, but don't forget Gag with the partition. 6. Print report header The above command output is more easier to divide between the name and the waistband level, or it can be divided into the domain between 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. It is also possible to add information heads to the 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. 7. Printing the information tail If you join END OF REPORT information on the end, you can use the E n D statement. 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 simple printhead information is completed and the query action is completed. 8. AWK error message prompts to be almost certain, and some errors will be encountered in the command when using A W K. 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 a double quotation number is lost, AWK will return: Chapter 9 AWK introduction 69 Download When using awk, you may be smashed by the error message, but through long and constant learning, Summarize the following rules. When you touch a W k error, you can find it according to: • 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: 9. How can I enter the file G R a d E. T X t, if there is no input file G R a d. The B e g i n part prints the file header, but A Wk finally stops the operation and waits, and does not return the S H E L L prompt. This is because A W K is expected to obtain 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
This treatment is not common because it is mostly used in a large number of prints. 9.2.3 Regular expressions in AWK and their operations In chapter G R e P, there are many examples to use regular expressions, which will not use the same example, but the conditional operation can be used to describe the usage of regular expressions in A Wk. The regular expression here is enclosed with a slash. For example, query string G R e e n in a text file, using / g r e e n / can detect the appearance of the word G R e e n. 9.2.4 Metacity Here is the characters that are often used in regular expressions in A Wk, for details, please refer to this book Chapter 7, as an overview. / ^ $. [] | () * ? There are two characters in Chapter 7, because they only apply to A W K and not for G R E P or S E D. They are: 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. 9.2.5 Condition Operation Table 9 - 2 gives the A W K condition operator, which will be given later. 70 Second Part Text Filter Download Table 9-2 AWK Condition Operator Operator Description Operator Description = Gad Oriented Oriented <= Less than or equal to Matching Regular Expression = = = 1! ~ Do not match the regular expression! = Not equal 1. Match To make a domain number match the regular expression, use the symbol '~' to closely follow the regular expression, or use the IF statement. Conditions of the after I F in A W K () enclose. Observe the document G R A D E. T X t, if the Print B R O W N waistband level can be seen that its domain is F I E L D - 4, which can write the expression {IF ($ 4 ~ / brown /) print} Application If F i E L D - 4 contains B R o w n, print it. If the conditions are met, the matching record line is printed. You can write the following script, because this is an action, must be enclosed in the parentheses {}. When the match is found, when it is not particularly declared, A w k defaults to print the entire record. It is a bit difficult to use the I F statement, but don't worry, because there are many ways to skip it and still maintain the same results. The following example is that if the record contains the mode brown, print it: 2. Exact match If you want to match the string, for example, check the student serial number 4 8, there are many student serial numbers in the file contain 4 8, if in Field - 3 Query No. 4 8, AWK will return a record of all serial numbers 4 8: To accurately match 4 8, use the equal number = =, and enclose the condition with single quotes. For example, $ 3 = "4 8", this ensures that only 4 8 serials can match, the rest can not. 3. Do not match the records that sometimes browse the information and extract the mismatch operation, with ~ The opposite symbol is! ~, Meaning does not match. Like the same matching operation with the query B R o W n belt level, now look at the mismatch. Expressions $ 0! ~ / Brown /, meaning that the query does not include the mode B R o W N belt level record and print it. Note that by default, A W k will print all match records, so there is no need to join the action section.
Chapter 9 AWK Introduction 71 Download You can only do not match the Field - 4, the method is as follows: If you only use the command awkings $ 4! = "Brown" {print $ 0} grade.txt, the error result will be returned, since the quotation number is enclosed BROWN, will only match 'brown without matching Brown - 2 and Brown - 3, of course, if you want to query non-Brown-2 waistband levels, you can do the following: 4. Don't see which students can get a chance . Test this is to determine whether the current level f i e l D - 6 is less than the highest score f i e l D - 7. In the output result, it is easy to join this change. 5. It is less than or equal to the comparison, less than or equal to doing some small changes only on the operator, and the records that meet this condition also include the output in the above example. 6. Big than big than the symbol everyone is familiar, please see example: I hope the reader has mastered the basic usage of the operator. 7. Set the case to query the case, you can use the [] symbol. When testing regular expressions, it is mentioned in [] or word, so if the level of the Query is Green Green, regardless of its large lowercase, the expression should be '/ [g g] reen /: 8 Any character extracts the name, which records the fourth character of the first domain is A, the use of the period .. Expressions / ^.. A / It means the first three characters of the first three characters, the fourth is A, the sharp corner symbol representative leads. 9. Or relationship matches the recording of the extraction level as Y E L O W or B R O W N, using a vertical album | It is intended to match and | one side of the two sides. Note that when using vertical characters, the statement must be enclosed in parentheses. 72 Second Part Text Filter Download The above example outputs a record of all levels Ye L O W or B R o W n. When using this method, when the query level is G R e E n or G R E E N, the same result as the [] expression can be obtained. 10. The head does not have to always use the domain number. If the query text file line includes 4 8 code, you can simply use the following ^ symbol: Here is how to use the expression involved in Chapter 7 in A W K. As mentioned in Chapter 7, all expressions (except for characters repeated) are legal in A W K. Composite mode or composite operator is used to form complex logic operations, and complexity depends on the programmer himself. It is important to understand that the composite expression is the expression between the plots by using each expression in the schema: && and: The statement must match the true. || o R: The two sides of the sentence matches the same. ! 非求 逆 11. And print record, make its name to 'p. b Unny and level Ye LLOW, use expressions ($ 1 = = "p. b unny" & $ 4 = = "ye llow") It means that & & & both sides are all true. The full command is as follows: 12. OR If the query level is YE L O W or B R O w n, the use or command is used. One or all of the matching mode of "| |" symbol is true. 9.2.6 AWK Built-in Variable A W K has many built-in variables to set environmental information. These variables can be changed. Table 9 - 3 shows some of the most commonly used variables and gives its basic meaning.
Table 9-3 AWK Built-in Variables Argc Command Line Parameters ARGV Command Line Parameter Arrangement Environment Environment Variables in the System Environment Variable Using FileName AWK Browse File Number FS Number FS Settings Input Domain Separator, equivalent to Command Row - F Option Chapter 9 AWK Introduction 73 Download (Continued) NF Browse Record Domain NR Credit Number OFS Output Domain Separator ORS Output Record Separator RS Control Record Sneerating ARGC Support Command Line Incoming AWK The number of parameters of the script. A R g V is a parameter array array of A R G C, wherein each element is represented as a command line parameters of the desired access. E N v i R O N Support system setting environment variable, to access individual variables, use actual variable names, such as E N V i R o N ["E D i to r"] = "vi". F i l e n M e supports an input file that A W K scripting actual operation. Because A w k can handle many files at the same time, so if you have access this variable, you will tell the system currently being browsed. F N r supports the number of records currently operated by A W K. Its variable is less than or equal to N r. If the script is accessing many files, each new input file will reset this variable. F S is used to set domain separators in A W k, which is the same as those in the command line - f option. By default, it is space. If you use a comma to create a domain separator, set f s = ",". N f supports the number of records, and then sets after the record is read. O f s allows the specified output domain separator, default is spaced. If you want to set it #, write O f s = "#". O R s is the output recording separator, default is new (/ N). R s is a record separator, default is a new row (/ n). 9.2.7 NF, NR and FILENAME Take a look at the A w k built-in variable. To quickly view the number of records, N R should be used. For example, after exporting a database file, if you want to quickly browse the record, it is compared to its initial state, and the error occurred during the export process. Use N R to print the number of records of the file. Print NR is placed in the E n D syntax. In the following example, all student records are printed and have their record numbers. Use the N F variable to display how many domains in each read record and print the input file name in the E n D part. When you extract information from a file, it is best to first check if there is a record in the file. The following example only queries the B R O W n level record when there is at least one record in the file. This feature is implemented using a A n d composite statement. After at least one record, query string B R o w n, and finally print results. 74 Part 2 Text Filter Download NF One Powerful Function is to pass the return value of the variable $ P W D to A W K and display its directory. Here you need to specify domain separator /. Another example is the display file name. 9.2.8 AWK operator uses operators in A W K, basic expressions can be divided into digital, string, variable types, domain and array elements, have been told in front. It lists its full list. Any of the following operators can be used in the expression. = = * = / =% = ^ = Assignment operator? Conditional Expression Operator || &&! And, with, Non (previous section) ~! ~ Match operator, including matching and mismatch <<= ==! = >> relationship operator - * /% ^ Arithmetic Operator - Prefix and the suffix have been described in the previous operation, and the following continues to describe the unintended part.
1. Set the input domain to the domain variable name in A W K, setting a meaningful domain name is a good habit, which is easier to understand when performing mode matching or relationship operation. The general variable name setting mode is N a m e = $ n, where n a m e is the called domain variable name, n is the actual domain number. For example, setting the learning domain name is N a m e, the level domain name is b E L t, and the operation is N a m E = $ 1; B E L T s = $ 4. Pay attention to the use of the semicolon, which is separated by a W k command. In the following example, the re-assay student name is N a m e, the level field is B E L T. The query level is the record of Ye L O W, and finally prints the name and level. 2. Domain value comparison operation There are two ways to test whether a value domain is less than another value domain. 1) Assign a value to the variable name in B e g i n. 2) Use the actual value in the relationship operation. It is usually assigned to the B e G I n part, and it can be reduced in a lot of trouble when the A Wk expression is changed. Use the relationship operation must be enclosed in parentheses. The following example queries a student scored from 2 7 points in all games. It is optional, "2 7" and 2 7 generates the same result with quotation marks. Chapter 9 AWK Introduction 75 Download the second example assignment to the variable name B A S E L i n E and the value assignment of the B E G i N gives the variable, both. 3. Modify the value of value domain Values When modifying any domain in A W k, it is important to remember that the actual input file is unmodified, and the modified only saved A w k replica in the cache. A W k reflects the modification traces in the variable N r or N F variable. To modify the numerical domain, the simple value to the domain identity, such as: $ 1 = $ 1 5, add the domain 1 value 5, but make sure that the assignment domain has a numeric value. Modify M. TA N S L E Y's current level segment, which is reduced from 4 0 to 3 9. Use assignment statements $ 6 = 6 - 1, of course, first match the domain name before implementing modification. 4. Modify the text domain Modify text domain to re-value it. What you need to do is given to a new string. The letter is added to J. TR OLL, make it j. L. Trull, the expression is $ 1 = "j. L. Triall", remember the string to use double second ("), and use round The brackets enclose the entire syntax. 5. Only the modified record is modified to a small file domain, so printing out all records to see the modified part is not a problem, but if the file is large, the record is even more than 1 0 0, printing all records just to view the modification Part is obviously unreasonable. Using the curly brackets after the mode will print only the modified portion. The acquisition mode, then perform operation according to the pattern results, some abstraction, exempted, only print the modified part. Pay attention to the position of the curly bracket. 6. Creating a new output domain When processing data in A W k, the creation of a new domain is a good habit of creating a new domain based on each domain. Creating a new domain To give a new domain identifier over other domains. If you create an additional new domain of other domains {$ 4 = $ 2 $ 3}, this assumes that the record contains 3 domains, and the domain 4 adds results for new domains, save domain 2, and domain 3. Creating a new domain 8 save the subtractivity value of the current level of the domain with the highest level of the domain in the file G R A D. The expression is '{$ 8 = $ 7 - $ 6}', the syntax first test domain current level points less than the domain's highest level. The new field is therefore printed with a student name and its new domain value greater than zero. Add a T A B button to the B E G I n part to align the header. 76 Part 2 Text Filtering Downloads Of course, new domains can be created, and give them a more meaningful variable name.
For example: 7. Increase the column value to increase the number of columns or run the results statistics, use symbol =. The increased results assigned to the left variable value of the symbol, and the domain added to the variable is on the right side of the symbol. For example, add $ 1 to T O T a L, the expression is T O t a l = $ 1. The value of the column is also useful. Many files require total statistics, but output their statistics is very cumbersome. This is very simple in A Wk, please see the example below. Putting all students' 'current levels', the method is T o T = $ 6, T o T is the sum of the domain 6 results of the entire file browsed by A W k. After all records are read, some prompt information and domain 6 are added to the E n D part. It is no longer necessary to display all records in A W K, and each operation matches, this is the default action. If the file is large, you just want to print the result portion instead of all records, plus parentheses () in the outside of the statement (). 8. When the file length is added to view the file in the directory, if you want to quickly view the length and sum of all files, you want to exclude subdirectory, use the ls -l command, then the pipe output to awk, AWK first eliminates the first character D (Using a regular expression), then add the file length column, and output each file length and the length of all files in the END portion. In this case, you first use the ls -l command to check the file properties. Note that the second file attribute is D, indicating that it is a directory, the file length is column 5, and the file name is column 9. If the system is not such a file name and its length, it should be changed in a timely manner. The following regular expressions indicate that the row must be matched, and the character D is excluded, the expression is ^ [^ D]. Use this mode to print the file name and its length, then add each length into the variable t O t. Chapter 9 AWK Introduction 77 Download 9.2.9 Built-in String Functions A W K has many powerful string functions, see Table 9 - 4. Table 9-4 AWK built-in string function GSUB (R, S) uses S to replace RGSUB (R, S, T) throughout $ 0, returning to S, S, T, T) returns the string T The first location Length (s) returns the S length Match (S, R) Test S if the Split (S, A, FS) containing the match R, and is divided into sequence asprint (FMT, Exp) to return FMT Formatted Expsub (r, s) Replace SSUBSTR (S, P) with the leftmostmost sub-string in $ 0 Returns Substr (S, P, N) returns a string from P starting from the P, N) Symphoreaver S-start length N is a bit similar to SED search and replacement. It allows replacement of a string or character as another string or character, and is performed in the form of a regular expression. The first function acts on the record $ 0, and the second G S u b function allows you to specify the target, however, if the target is not specified, default is $ 0. The I n d e x (s, t) function returns the first position of the query string T in the target string S. l E n g t h function returns the character length of the string S character. The M a T C h function test string S contains a match definition of a regular expression R. S P L i T uses domain separator f s to divide the string S into the specified sequence A. The S P R I n t function is similar to the P R I n t F function (later involved), returns the resulting string E x P of the basic output format F m t.
The S u b (r, s) function will use S to instead of the leftmostmost sub-string in $ 0, which matches (R). S U b (s, p) returns the suffix of string S after position P. S UB S T R (S, P, N) is the same, and the length of the sub-string is n. Take a look at the functionality of these string functions in A Wk. 1. GSUB replaces a string throughout the record to another, using regular expression format, / target mode /, replacement mode /. For example, changing Student Series 4 8 4 2 to 4 8 9 9: 2. Index Query String S-A appears in the first position. The characters must be enclosed in a double quotation mark. For example, returning to the first position of N y in the target string B u n y, that is, the number of characters. 3. Length returns the desired string length, such as inspection string J. Tr O L L returns the name and its length, the number of characters composed of human name. There is also a method, here the string plus double quotes. 78 Second Part Text Filter Download 4. Match M A T C H Tests whether the target string contains a part of the lookup character. You can use a regular expression on the lookup section, and the return value is the number of characters that successfully appear. If not found, return 0, the first example finds D in A N C D. Because it does not exist, it returns 0. The second example finds D in A N C D. Because of its existence, returns the number of first characters that appear in A N C D. The third example is found in the student J. L u L u. 5. Split Use S P L i T Returns the number of character strings array elements. The working mode is as follows: If there is a string, a specified separator is included - such as A D2 - K P 9 - J U 2 - L P - 1, divides it into an array. Use S P L i t, specify the separator and the array name. In this case, the command format is ("A D 2 - K P 9 - J U 2 - L P - 1", P A R T, S P1, then returns the number of arguments, which is 4. There is also an example using different separators. In this example, S P L i t returns the number of subscripts of the array M y A r R R. The value of the array M y A r r a y is as follows: The end of this chapter tells the architecture concept. 6. SUB uses S u b to discover and replace the first appearance position of the mode. String S t r contains 'Poped POPO Pill', performs the following S u b command S u b (/ O P /, "O P", S T R). Mode O P first appears, replace the operation, the return result is as follows: 'Po Ped Pill Pill'. In this chapter text file, students j. Tr o L L have two values, "current level points" and "highest level points". Only the first one is 2 9, the second is still 2 4, and the operation command is S u b (/ 2 6 /, "2 9", $ 0), only replacing the first position of 2 4 appears. Note j. Tr O L L needs to exist. 7. SubStr S UB S TR is a very useful function. It returns a part of the string according to the starting position and length. The example is as follows: In the above example, specify that the first character of the domain 1 begins, returns 5 characters in front of it.
Chapter 9 AWK Introduction 79 Download If the given length value is much larger than the string length, the awk will return all characters from the starting position. To extract the last name of L TA NSL - EY, simply return the length from the third character to 7 . You can enter the length 9 9, and the A W k return results are the same. Another form of S UB S T R R is back to the string suffix or the specified position behind characters. Here you need to give the specified string and its starting position thereof. For example, the last name is extracted from the text file, and the domain 1 is required, and from the third character: There is also an example, the string is defined in the B E g i n part, returns a substring extracted from the tip of the e n D. 8. Introducing the A W K in S H E L L This chapter has been mentioned that A W K scripts are only one line, where few are character strings are represented. This book is required to complete A W K scripts in a line, which is easy to pass the variable into a W k command line. Some examples have been described on their basic principles. Use the pipe to pass the string S t a n d - b y in A w k, return its length. The setting file name is a variable, the pipe outputs to A w k, returns the file name without the extension. The setting file name is a variable, the pipe outputs to A w k, and only returns its extension. 9.2.10 String Sequence When using a string or regular expression, sometimes you need to add a new line or query one yuan characters in the output. When printing a new line, (New Behavior Character / N) gives its shielding sequence to do not lose its special meaning, usage is to add a backslash in front of the string. For example, use / N to force print a new row. If you use a regular expression, query the currency ({}), add the diagonal line before the character, such as / / {/ will lose its special meaning in A W K. Table 9 - 5 Lists other shields sequences for AWK Identification Sequences 9-5 AWK Used Mask Sequence / B Return Key / TTAB Key / F Paper Change Page / DDD Octa-Binary Value / N New Row / C Arbitrary Other Special Character, for example / / / / for the second part of the backlash symbol / R Enter key 80, the second part of the text filter is downloaded using the above symbol, prints the May Day, the middle clip Tab button, and then follow the two new rows, then print May Day, but this time Octa 1 0 4, 1 4 1, 1 7 1, represents D, A, Y, respectively. Note that / 1 0 4 is the octal A S C I I code of D, / 1 4 1 is an octal A S c I I I code, and the like. 9.2.11 AWK Output Function Printf So far, all examples of output are directly to the screen, except for the T A B key, there is no format. A W K provides a function P R I n t f, has several different formatting output functions. For example, by column output, left align or right alignment mode. Each P R I n t f function (format control character) starts with a% symbol, ending with a decision-converted character. The conversion contains three modifiers. P R I n t F function Basic syntax is P R i N t f ([Format Control], parameter), and format control characters are usually in quotation marks.
9.2.12 Printf Modification Table 9-6 AWK PrintF Modifier - Left Align Wi DTH Domain Step Length, use 0 to represent 0 steps. PREC maximum string length, or number of bits on the right side of the decimal point 9-7 awk Printf format % C ASCII character% D integer% E floating point number, scientific counting method% F floating point number, for example (1 2 3. 4)% Gawk decided to use which floating point number conversion E or F% O 8 feed number% s string % x hexadecimal number 1. Character conversion observes the equivalent value of 6 5 in the ASCII code. Pipe output 6 5 to A w k. P R i n t f performs A S c i i code character conversion. This is also added here, because PR i n t f does not do a changing action by default. Of course, the same way can also be used in the same manner. All character conversion is the same, the following example represents the output result of '9 9 9' after the floating point number conversion. Six decimal points were added after the integer was introduced. 2. Format Output Chapter 9 AWK Introduction 81 Download All student names and serial numbers, require the name left alignment, 1 5 characters length, followed by the serial number. Note / n backwards behind the last indicator. The output will be automatically divided into two columns. It is best to add some text annotations to help understand the message enlightenment. The header information can be embedded in front of the body. Note that the header information is added here using P R i n t. If you prefer, P R i N T f can also be used. 3. Command the value to a line A w k before viewing A W k scripts, first check how to transfer variables in the A W k command line. When A W k execution, the value is passed to a W k variable, and the variable needs to be placed in the command line, the format is as follows: awk command variable = Enter the file value (how to pass the variable to the A Wk script). The following example sets the variable A g e equal to 1 0 in the command line, and then incorporates all students under A W K, and all students under the age of 1. To quickly view the file system space capacity, observe whether it reaches a certain level, you can use the following A W K a row script. Because the useful space capacity to be monitored is constantly changing, you can specify a trigger value at the command line. First use the pipe command to pass DF -K to A w K, then extract the fourth column, that is, the remaining available spatial capacity. Use $ 4 ~ / ^ [0 - 9] / acquisition capacity (1 0 2 4) instead of the DF file header, then the value specified in the command line with the 'IF ($ 4 Like the original, the students currently level plus AWK '(T O T = $ 6) end {print "Club Student Total Points:" T O T}' g R A D E. T x T. Create a new file S t u d e n t_ t o t. A w k, add a WK extension to all A W K programs to be a good habit, which is a WK program by checking the file name. The text is as follows: The first line is! / bin / awk -f. This is very important, without its own script will not be executed. This line of the script system is the location of A W K in the system. By separating the command, the script readability is improved, and a comment can be added between the commands. Here, the average of the header information and the end is added. Basically this is a row of script files. When executed, type the input file name after the script file, but first add the executable permissions to the script file. The account verification program used in the system verifies the data input of the data operator. Unfortunately, this program has a little error, or it should be said to be "non-text feature". If a record is found to contain an error, it should only print only one line of chapter 9 AWK introduction 83 Download "E R R O R *", but in fact, many such errors are printed. This will cause misconception to the account administrator, so you need to filter the error line with the A W K script, so that each failure record can only correspond to an error line. Take a look at some files before the AWK is implemented: awk scripts are as follows: AWK Filtering results are as follows: 5. Use FS variables in awk If you use non-free domain separator (FS) to browse files, such as # or:, write such One ordered command is easy, because the domain separator can be specified in the command line using the FS option. 84 Part 2 Text Filter Download When using a W k script, remember to set the F S variable is part of the B E G I n. If this is not done, A w k will be confused, and I don't know what the domain separator is. The following script specifies the F S variable. The script extracts the first and 5th domains from the / e T C / P A S W D file, separated by semicolon ";" separated the P A S W W D file domain. The first domain is the account name, and the 5th domain is the account owner. 6. The A w k script pass value is substantially the same as the A W K scripting value, the format is: awk script_file var = value input_file The following script comparison check files. Here, the n f variable M a x is used, indicating the domain number of the specified check, encloses the domain separator using dual quotation, even if it is a space. If the input file is performed in / e t C / P A S W W D (7 domains in the P A S s W D file), run the above script. The parameter format is as follows: Use the previous line script to convert it to a W k script as follows: Text includes more information than the actual command, it doesn't matter, after studying the text, you can accurately know its function and how to call it. Don't forget to increase the executable permissions of the script, then put the variables and assignments After entering the file, enter the file before entering the file. Chapter 9 AWK Introduction 85 Download The same can be used, and the following A w k scripts get input from the D u command, and output blocks and bytes. To run this script, use the D u command, and output to a W k script. 9.2.13 AWK array describes how to use the SP L i t function in front of how to use it into an array of elements.