Linux text processing tool
1. awkawk is a powerful text analysis tool. Compared with grep search and sed editing, awk is particularly powerful in data analysis and report generation. Awk is equivalent to a programming language and has many of its own syntaxes. However, the syntax is very simple. Similar to the C language, syntax elements include logical comparison, if, built-in variables, string processing functions, and arrays, loop statement. To put it simply, awk refers to reading files row by row. Each line is sliced with spaces as the default separator, and the cut part is analyzed and processed. During awk execution, Its browsing is marked as $1, $2... $ n. This method is called domain tag. Use $1, $4 to refer to the 1st and 4th domains. Note that the fields are separated by commas (,) and $0 is used to indicate that all domain processing options are
awk '{pattern + action}' {filenames}
Example:
Last-n 3 | awk '{printf ("%-6 s % s \ n", NR, $1)}'/* Resolution: The NR built-in variable indicates the number of read records, $1 indicates the number of parameters after segmentation, and $0 indicates the entire record */cat/etc/passwd | awk-F ': ''in in {user_count = 0} {printf ("%-5d %-20 s % s \ n", ++ user_count, $1, $7 )} END {print "Found" user_count "users \ n"} '/* Resolution: Use the-F option to specify the separator as': '. The command options are divided into three parts, the first part is "BEGIN {user_count = 0}". Before Running AWK, this command defines a variable to count the number of users; the second part is "{printf (" %-5d %-20 s % s \ n ", ++ user_count, $1, $7 )}", run this command after each awk record. The third part is "END {print" Found "user_count" users \ n "}". This command is finally executed, number of output users */awk-F' # ''{printf (" % 8 s % 6 s % 16 s % 8s \ n ", $1, $2, $3, $4)} '1.log/ * parsing: This command uses '#' to split and process files 1. output the text content in the format of each line in log. The content of the file to be processed is as follows: name # age # date # addressliuxiang #14 #2014-05-23 # cnTom #24 #2013-09-12 # uslucy #18 #2011-02-01 # uk */awk-F' # ''{if ($2> = "18 ") {printf ("% 8 s % 6 s % 16 s % 8s \ n", $1, $2, $3, $4)} '1.log/ * Resolution: output records with age greater than or equal to 18 */awk-F' # ''{record [1] = $1; record [2] = $2; record [3] = $3; for (I in record) {printf ("%-16 s", record [I]);} printf ("\ n ");} '1.log/* parsing: Build an array and output it using cyclic statements */
An example of batch file name modification: You need to remove files in the current directory with the same prefix as file _.
# Example of batch file name modification # automatically generated file echo good | awk '{for (I = 0; I <20; I ++) {s = "touch file_no.txt "; gsub ("no", I, s); system (s)} '# Get the current file list ls-l | awk' {if ($9! = "") Print $9} '# modify files in batches. Note: get the file list and filter out unnecessary files, gsub string operation function ls-l | awk '{if ($9! = "") Print $9} '| awk'/^ file/{src = $0; gsub ("file", ""); cmd = "mv src dst "; gsub ("src", src, cmd); gsub ("dst", $0, cmd); system (cmd );}'