awk Usage II

Source: Internet
Author: User
Tags arithmetic operators

3. The printf command of awk

Format: printf format, item1 item2 ...

Points:

(1) to specify format;

(2) Output will not be automatically wrapped, if you need to change the line must be given \ n;

(3) format is used to specify its output format for each subsequent item;

(4) The format indicator is a% beginning, followed by a character.

Characters commonly used in format:

%c: ASCII code for displaying characters

%d,%i: decimal integer;

%e,%e: Scientific counting method;

%f: Displays floating-point numbers;

%g,%g: Display values in scientific notation format or floating-point number format;

%s: Display string;

%u: Displays unsigned integers;

Percent: show% itself.

Common modifiers in format:

#: Display width;

-: left-aligned;

+: Display the symbol of the value;

. #: Value precision.

Example 1: The user name and shell information for the first 10 records in the/etc/passwd file with a fixed width of 20 output:

650) this.width=650; "title=" 1451027583534543.jpg "alt=" 1.jpg "src=" http://www.178linux.com/ueditor/php/upload/ Image/20151225/1451027583534543.jpg "/>

The partial meaning of the above example is that two fields are displayed with a width of 20 characters, and a newline character is the Terminator for each line.

Example 2: Display the user name in the previous example as left-justified:

650) this.width=650; "title=" 1451027891544274.jpg "alt=" 2.jpg "src=" http://www.178linux.com/ueditor/php/upload/ Image/20151225/1451027891544274.jpg "/>

Example 3:3.14159 takes 2 decimal places to display:

650) this.width=650; "title=" 1451028126980072.jpg "alt=" 3.jpg "src=" http://www.178linux.com/ueditor/php/upload/ Image/20151225/1451028126980072.jpg "/>

4. Awk's operator

Arithmetic operators:

X+y: Plus

X-y: Minus

X*y: Multiply

X/y: except

x** (^) y: exponentiation

X%y: Take the mold

-X: Negative value

+x: Convert to Numeric

String operators:

Connection: No need to sign, directly put two strings together on it;

Assignment operators:

X=y: Assigns the value of Y to X;

X+=y: Assigns the result of the x+y to X;

X-=y: The X-y result is assigned to X.

X/=y: The result of dividing x by Y is assigned to X;

X%=y: Assigns the result of X and y to x;

X^=y or x**=y: assigns x to the result of the Y-order to X;

X + +: Assigns the result of the x+1 to X.

x--: Assigns the result of the x+1 to X;

Note: If the mode itself is the = sign, write as/=/

Comparison operators:

X<y: True if x is less than y;

X<=y: True if X is less than or equal to Y;

X>y: True if x is greater than Y;

X>=y: True if x is greater than or equal to Y;

X==y: True if x equals y;

X~y: Pattern matching, x can be matched to true by Y mode.

Logical operators:

X&&y: Executes y if x is true;

x| | Y: Executes y if X is false.

Conditional expression:

Selector? if-ture-expression:if-false-expression

Selector: Conditions

If-ture-expression: This expression is executed if true

: if-false-expression: Otherwise execute this expression

Example 4: Judging that the UID in the/etc/passwd file is greater than 500 is displayed as "Common user", otherwise it will appear as "Admin user":

650) this.width=650; "title=" 1451031871504107.jpg "alt=" 4.jpg "src=" http://www.178linux.com/ueditor/php/upload/ Image/20151225/1451031871504107.jpg "/>

5. Mode

(1) Regular expressions

Format:/pattern/

Example 5: Displays only the lines in the/etc/passwd file that are rooted:

650) this.width=650; "title=" 1451032329498004.jpg "alt=" 5.jpg "src=" http://www.178linux.com/ueditor/php/upload/ Image/20151225/1451032329498004.jpg "/>

(2) expression

The majority of the comparison expressions, which result in a non-0 or non-empty string, satisfy the condition, and awk only handles rows that satisfy the condition.

Example 6: Only rows with uid greater than or equal to 500 are displayed:

650) this.width=650; "title=" 1451032503683123.jpg "alt=" 6.jpg "src=" http://www.178linux.com/ueditor/php/upload/ Image/20151225/1451032503683123.jpg "/>

(3) Special mode

BEGIN: Executes once before the program of the awk command is run;

END: Executes once after the program of the awk command is run.

Example 6: Displays the user name of the line in the/etc/passwd file that starts with root in the person, UID, shell information, displays the fields separated by commas, and prints "the end" after the result is output.

650) this.width=650; "title=" 1451033850450162.jpg "alt=" 7.jpg "src=" http://www.178linux.com/ueditor/php/upload/ Image/20151225/1451033850450162.jpg "/>

(5) Empty mode

Used to match any row.

650) this.width=650; "title=" 1451034170926634.jpg "alt=" 8.jpg "src=" http://www.178linux.com/ueditor/php/upload/ Image/20151225/1451034170926634.jpg "/>


awk Usage II

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: info-contact@alibabacloud.com 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.