Regular Expressions for Shell Learning 2----awk

awk

Sed processes files in line units, and awk is stronger than sed in that it can process files not only in line units but also in column units. The default line separator of awk is newline, and the default column separator is continuous space and Tab , but both line separator and column separator can be customized. For example , each line of the /etc/passwd file has several fields. The fields are separated by : , and you can redefine the column separator of awk as : and process the file in column units. Awk is actually a very complex scripting language with branching and looping structures like C , but the basic usage is similar to sed . The basic form of the awk command line is:

awk option 'script' file1 file2 ...

awk option -f scriptfile file1 file2 ...

Like sed , the files processed by awk can either be redirected from standard input, or passed in as command-line parameters. Editing commands can be passed in directly as command-line parameters, or you can specify a script file with the -f parameter to edit commands. The format is:

/pattern/{actions}

condition{actions}

Similar to sed , pattern is a regular expression, and actions are a sequence of actions . The awk program reads out the file to be processed line by line. If a line matches the pattern or satisfies the condition , the corresponding actions are executed . If an awk command has only the actions part, the actions act on each line of the file to be processed. For example, the content of the file testfile indicates the inventory of a store:

ProductA 30

Product B 76

ProductC 55

Print the second column of each row :

$ awk '{print $2;}' testfile

30

76

55

The automatic variables $1 and $2 represent the first column, the second column, etc., respectively, similar to the positional parameters of shell scripts, while $0 represents the entire current line. For another example, if the inventory of a certain product is less than 75 , it will be marked at the end of the line that it needs to be ordered:

$ awk '$2<75 {printf "%s\t%s\n", $0, "REORDER";} $2>=75 {print $0;}' testfile

PRODUCT 30 REORDER

Product B 76

ProductC 55 REORDER

It can be seen that awk also has a printf function that is very similar to the C language . The condition part of the awk command can also be two special conditions - BEGIN and END . For each pending file, the actions after BEGIN are executed once before the entire file is processed, and the actions after END are executed once after the entire file is processed.

The awk command can use variables like the C language (but does not need to define variables), such as counting the number of blank lines in a file

$ awk '/^ *$/ {x=x+1;} END {print x;}' testfile

Like shell environment variables, some awk variables are predefined with special meanings:

awk commonly used built-in variables

FILENAME 当前输入文件的文件名,该变量是只读的

NR 当前行的行号,该变量是只读的,R代表record

NF 当前行所拥有的列数,该变量是只读的,F代表field

OFS 输出格式的列分隔符,缺省是空格

FS 输入文件的列分融符,缺省是连续的空格和Tab

ORS 输出格式的行分隔符,缺省是换行符

RS 输入文件的行分隔符,缺省是换行符

例如打印系统中的用户帐号列表

$ awk 'BEGIN {FS=":"} {print $1;}' /etc/passwd

awk也可以像C语言一样使用if/elsewhilefor控制结构。可自行扩展学习。

awk:
Format: 1. awk parameter 'script statement(/pattern/{actions})' file to be operated

2. awk parameter -f 'script file' file to be operated

printf("%s\t%s\n", $11 , $2); print $11 $2 BEGIN: Do something before pattern does not match the file. awk 'BEGIN {FS=":"} {print $7}' /etc/passwd awk -F: {print $7} /etc/passwd END: pattern did not match end of file, do something. ps aux | awk '$2>6000 && $2<=7000 {num=num+1} END {print num}'  ps aux | awk 'BEGIN {FS=" "} $2>6000 && $2<=7000 {x=x +1} END {print x}'  ps aux | awk -F" " '$2>6000 && $2<=7000 {x=x+1} END {print x}'
















Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324603949&siteId=291194637