Awk Report Generator

Source: Internet
Author: User

Awk Report Generator

I. awk concepts and use of formats

Awk is a powerful report generator, awk itself has the ability to traverse, support the conditional loop, is a programming language, awk output 1, the Print command followed by the items are separated by commas, and output using the output character segmentation. 2, the output is a string or numeric value, when the record field, variable or awk expression: The value will be implicitly converted to a string after the output. 3, after print if omitted equivalent to the output blank print "". 4, display the current system. If the Print after option does not have a comma, only the result of the whitespace output is serialized together {print $ $7}

1 , AWK variables are divided into built-in variables and custom variables

FS : Field Sperator when entering fields separator

#awk ' begin{fs= ': '}{print $1,$7} '/etc/passwd

RS : The row delimiter when the Record seperator input is the line break

#awk ' begin{rs= ': '}{print $1,$7} '/etc/passwd

Ofs:output filed Seperator field delimiter at output

# awk ' begin{ofs= ': '}{print $1,$7} '/etc/passwd

ors:output ROW serperator Output Rows Separator

#awk ' begin{ors= ': '}{print $1,$7} '/etc/passwd

Nf:number of FIELD Number of fields

# awk ' begin{fs= ': "}{print NF $1,$7} '/etc/passwd

Nr:number of RECORD number of rows, all files counted together

# awk ' begin{fs= ': "; ofs=": "}{print nr,$1,$7} '/etc/passwd

FNR: multiple files when each file counts

#awk ' begin{fs= ': "; ofs=": "}{print fnr,$1,$7} '/etc/passwd/etc/group

ARGV: the array saves the character of the command itself. ARGC: Preserves the number of awk parameters

#awk ' Begin{print argv[0],argv[1] ARGC} '/etc/passwd/etc/group

awk A variable that can be customized in the format of-v Var_name = VALUE is case-sensitive in this variable name.

#awk ' begin{a= ' Hi awk "}{print A} '/etc/group how many rows in this/etc/group will output how many lines Hi awk, if you want to output a line #awk ' begin{a= ' Hi awk"}{print A} ' You can also #awk-v a= "Hi awk" ' Begin{print A} '

1.2 , awk's printf command point of Use: 1) does not wrap to the specified format;2), and the newline is executed \n;3) format is used to specify its output format for each subsequent item. The format indicator is preceded by a character, and%c: Displays the character ascii;%d,%i: decimal integer;%e,%e: scientific notation displays values;%f: Displays floating-point numbers;%g:%g displays values in scientific notation or floating-point format;%s: display string; %u: shows no strings; Percent: displays% itself.

1.2.1 ,% can be followed modifier: #: Indicates the display width;-: Left alignment; +: Displays the symbol for the value. For example,%-10s: Represents a width of 10 characters and is left-justified.

#awk-F: ' {printf% ' 10s,%20s\n ', $1,$7} '/etc/passwd, the output result is separated by commas of course can also be separated by a space #awk-f: ' {printf '%10s%20s\n ', $1,$7} '/etc/ passwd commas into spaces. You can also align the left side of the right #awk-f: ' {printf% ' 10s%-20s\n ', $1,$7} '/etc/passwd. Let's take a look at the operation of floating-point numbers.

650) this.width=650; "id=" Big_pic "alt=" wkiom1r7pldigzoqaabwllord-a251.jpg "src=" =600&h=600&t=f&url= "/>

As the first line of the above does not have a line break, and two or three lines add line break, its effect is clear, beautiful and generous. The most important thing is the ability to retain a valid bit. Here's another look at the use of%e and%f.

650) this.width=650; "alt=" wkiol1r7ptmhurkpaaboykk9xz4759.jpg "src=" 600&t=f&url= "/>

1.2.2 , awk operator has arithmetic operator +,-,*,/,%,-x (negative value), +x (converted to numeric). Assignment operators: =,+=,-=,/=,%=,^= or **=,++,--。 If the pattern itself is =, write/=/. Comparison operators: <,>,<=,>=,==,+=,!=,~: pattern matching, the left string is true by matching the pattern on the right, otherwise false. ~=: On the contrary. Logical operator: &&,| |. Conditional expression: selector?if-true-expression:if-false-expression. The function calls Function_name (ARGU1,ARGU2). The conditional expression is applied as follows.

650) this.width=650; "alt=" wkiom1r7plgskrdkaaga4ooiqri510.jpg "src=" 600&t=f&url= "/>

2 , awk mode

2.1 Regrexp : The format is/pattern/and only the rows that are matched to by/pattern/are processed. As # awk-f: '/root/{print $} '/etc/passwd

2.2 espression: An expression that satisfies a condition when the result is not 0 or a non-empty string, processing only satisfies the condition, for example: # awk-f: ' {printf '%10s%-10d\n ", $1,$3} '/etc/passwd.

2.3 Ranges : Row range, previous address delimitation, startline,endline, processing only rows within the row range.

2.4 Begin/end: Special mode, only before the program of the awk command runs (BEGIN) or after run (END)

2.5 Empty : Empty mode, matching any row.

3. common action:1) expressions, such as assignment expressions, 2) can also be control statements, 3) HA can be a combination statement, 4) input statements, 5) Output statements (print, printf).

4 , detailed control statements

4.1 if-else , format if (condition) {then body} Else{else Body} For example: # awk-f: ' {if ($3>=500) {print $, ' is a common user '}else{print $, ' I s an admain or system user. "}} ' /etc/passwd. Look at one example: Find the line in/etc/inittab with a number of fields greater than or equal to 8, # awk ' {if (nf>=8) {print}} '/etc/inittab

4.2 while Loop, Format: while (condtion) {while body} condition is true into the loop. For example, the output/etc/inittab under the odd number of characters of line # awk ' {i=1;while (I<=NF) {printf "%s", $i; i+=2};p rint ""} '/etc/inittab.

Length () function: Take the length of the string. For example, to remove characters that are greater than 6 characters in/etc/inittab: # awk ' {i=1;while (I<=NF) {if (length ($i) >=6) {print $i};i++}} '/etc/inittab

4.3 for Loop: Its format: for (variable assignment; condition;iteration process) {for Body}, example # awk ' {for (i=1;i<=nf;i+=2) {printf '%s ", $i};p rint" "} '/etc/inittab is the line that finds the odd string under/etc/inittab. Then look at the use for loop to remove characters greater than 6 characters in/etc/inittab: # awk ' {for (i=1;i<=nf;i++) {if (length ($i) >=6) print $i}} '/etc/inittab

for loops can be used to iterate over an array element: The syntax format for (i in array) {for body}. The following is an instance of # awk ' Begin{for (i=1;i<=5;i++) {array[i] = i*2-1;} For (i in array) {print I "=" Array[i]}} '






are not output sequentially.

4.4 Case statement, Syntax format: switch (expression) {case VALUE or/rgeexp/:stateme1;...default:staemen}

4.5 Next Indicates that the processing of the bank has been terminated prematurely and entered the next line. Example: # awk-f: ' {if ($3%2==0) next;print $1,$3} '/etc/passwd, which means that the line that satisfies the condition is not processed.

5 , an array, if a reference to an array element does not exist beforehand, awk automatically creates the element and initializes it to an empty string, so to determine whether an array has an element, use the index in array format. If you want to repeat each element in the group, you need to use the following special structure. For (var. array) {for body}, where

# Netstat-tan | awk '/^tcp/{++state[$NF]}end{for (s in state) {print S state[s]}} '

6 , Awk's built-in functions, Split (String,array[,fieldsep[,seps]) function: The string represented by strings is sliced with filedsep as a separator. And the result of the slice is saved to an array of arrays named: awk ' Begin{split ("root:x:0:0", User, ":"); for (I-in user) print User[i]} '

In its internal save User[1] is root,user[2] is x,user[3] is 0,user[4] is 0.

Awk Report Generator

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.