1.awkawk is a powerful text analysis tool, with the search for grep and the editing of SED, which is especially powerful when it comes to analyzing data and generating reports. Awk is the equivalent of a programming language with a lot of its own syntax, but the syntax is simple, similar to the C language, with syntax elements such as logical comparisons, if, built-in variables, string-handling functions, arrays, loop statements, and so on. To put it simply, awk reads the file line-by-row, using spaces as the default delimiter to slice each row, and then perform various analytical processing of the cut. When Awk executes, its browse flag is $1,$2 ... $n, this method is called a domain tag. Using $1,$4 to refer to fields 1th and 4th, note that this is a comma-delimited field, using the $-A to indicate that all domain processing options are used
awk ' {pattern + action} ' {filenames}
Examples of use are:
Last-n 3 | awk ' {printf ("%-6s%s\n", NR, $)} '/* * Parse: NR built-in variable represents the number of read records, a similar representation of the first few parameters after splitting, "$" for the entire record */CAT/ETC/PASSWD | Awk-f ': ' BEGIN {user_count=0} {printf ("%-5d%-20s%s\n", ++user_count, $ $7)} END {print "Found" User_count "Users \ n "} '/* Parse: Use the-f option to specify the delimiter as ': ', the command option is divided into three parts, the first part is" BEGIN {user_count=0} ", executed before awk execution, the command defines a variable to count the number of users, and the second part is" {printf ("%- 5d%-20s%s\n ", ++user_count, $ $7)}", each record awk executes the command after, and the third part is "End {print" Found "User_count" users\n "}", this command last executed, output the number of users */ Awk-f ' # ' {printf ("%8s%6s%16s%8s\n", $, $ $, $ $, $4)} ' 1.log/* parse: The command uses ' # ' to split each line in file 1.log, and handsome Select output text content in format, where the contents of the processing file are as follows: N Ame#age#date#addressliuxiang#14#2014-05-23#cntom#24#2013-09-12#uslucy#18#2011-02-01#uk*/awk-f ' # ' {if ($ >=) {") {printf ("%8s%6s%16s%8s\n ", $, $, $ $, $4)} ' 1.log/* parse: output age greater than or equal to 18 records */awk-f ' # ' {record[1]=$1; record[2]=$2; record[3]=$3; For (i-in record) {printf ("%-16s", Record[i]);} printf ("\ n");} ' 1.log/* Parsing: Building an array, using the Loop statement output */
Linux Text Processing Tools