AWK is a column-based text processing tool that works by reading text on a row and treating it as a record, dividing each record into fields into fields, and then outputting the values of the fields, in fact, awk is a programming language. Awk considers files to be structured, meaning that they consist of words and various whitespace characters, where the ' whitespace ' includes spaces, tabs, and continuous spaces and tabs. Each non-blank part is called a field, from the right to the first domain, the second domain, and so on. $, $, respectively, are used to represent the domain, and $ A for all domains.
First create the Awk.txt file, as shown in the following file:
John.wangmale30021-1111111lucy.yangfemale25021-2222222jack.chenmale35021-3333333lily.gongfemale20021-4444444shanghai
Print the specified domain
Now that Awk uses $1,$2 to represent a different domain, you can print the specified domain. Take Awk.txt's first line, the first field is John.wang, the second field is male, the third domain is 30, and the fourth field is 021-1111111. In the following demonstration, the first command prints the two fields, $4, and the second command prints all the fields.
#只打印姓名和电话号码awk ' {print $1,$4} ' Awk.txt
#打印全部内容awk ' {print $} ' Awk.txt
Specify print separators
By default, awk uses white-space characters as delimiters, but you can also specify delimiters by-F to differentiate between domains
#指定 "." As delimiters, so that each line is "." The previous character, the "." The next character, such as the first line, is "John", which is "Male 02101111111" awk-f. ' {print $1,$2} ' Awk.txt
Internal Variable NF
sometimes files are large, each row number is different, you must get the number of file columns in a particular way. This can be done simply by using AWK's internal variable NF. Of course, if you specify a different delimiter, the result may not be the same.
#使用默认分隔符awk ' {print NF} ' Awk.txt
#使用指定分隔符awk-F ' {print NF} ' Awk.txt
Print Fixed field
with internal variables, you can simply get the number of columns per row, and if you precede the NF with the $ symbol, the last column will be represented, so that no matter how many columns each row has, you can print the last line with $NF.
#打印最后一列awk ' {print $NF} ' Awk.txt
#打印倒数第二行awk ' {print $NF} ' Awk.txt
Intercept string
You can use the substr () function to intercept a string for the specified domain, which is used in the following way:
SUBSTR (the position of the specified field, the first start character, the second ending position), where the second end of the position can be empty, so that the default output to the last character of the field
The following example outputs the contents of the sixth character of the first field of the Awk.txt file to the last character:
#注意, the second ending bit is omitted, so the end position is the last character of the first field cat Awk.txt | awk ' {print substr ($1,6)} '
Determine string length
The length of the string can be determined using the internal variable, as shown in the following example:
Cat Awk.txt | awk ' {print length} '
Using awk to find columns
structured data is very common, and in daily work, there is often a need to add data to it, and here are some calculations of age in Awk.txt. Notice that the age field is the third field.
#求年龄的和cat Awk.txt | awk ' begin{tolal=0}{total+=$3}end{print total} ' #求平均年龄cat Awk.txt | awk ' Begin{tolal=0}{total+=$3}end{print Total/nr} '
Linux Learning-awk Tools