Several common usages of awk under Unix

Source: Internet
Author: User
Tags arithmetic operators command line expression variables regular expression square root

1.awk Regular expression meta-character

\ code-changing sequence

^ Start match at beginning of string

$ start match at end of string

. Match any single string

[ABC] matches any character within []

[A-ca-c] matches characters (in alphabetical order) with A-c and A-c ranges

[^ABC] matches any character except all characters in []

desk| Chair matches any one of the desk and Chair

[ABC] [DEF] Association. Match any character in a, B, and C, and then follow any of the characters in D, E, and F.

* Match the characters that appear 0 or more times in either a, B, or C

+ matches any of 1 or more occurrences of a, B, or C

? Match an empty string or a, B, or C in any one character

(blue| Black) berry combined with regular expressions, matching blueberry or blackberry

2.awk arithmetic operators

operator purpose

------------------

X^y X's Y power

X**y ditto

X%y calculates the remainder of x/y (modulo)

X+y x plus y

X-y minus y

X*y x multiply y

x/y x except Y

-y negative y (switch symbol for y); also known as a mesh reduction

++y y plus 1 use y (front Plus)

y++ use Y value and add 1 (suffix Plus)

--y y minus 1 use Y (front minus)

y--after use y minus 1 (suffix minus)

X=y assigns the value of Y to X

X+=y assigns the value of X+y to X

X-=y assigns an X-y value to X.

X*=y assigns the value of X*y to X

X/=y assigns the value of x/y to x X%=y assigns the x%y value to X

X^=y assigns the value of X^y to X

X**=y assigns the value of X**y to X

3.awk allowed Tests:

operator meaning

X==y x equals y

X!=y x is not equal to Y

X>y x is greater than Y

X>=y x is greater than or equal to Y

X<y x is less than Y

X<=y x is less than or equal to Y

X~re x matching Regular expression re

X!~re x mismatch Regular expression re

4.awk operators (in ascending order of priority)

=, + =, =, *=,/=,%=

||

&&

> >= < <= = =!= ~!~

XY (string link, ' x ' y ' becomes "xy")

+ -

* / %

++ --

5.awk built-in variable (predefined variable)

Description: The V item in the table represents the first tool to support the variable (same below): A=awk,n=nawk,p=posix Awk,g=gawk

V variable meaning default value

--------------------------------------------------------

N ARGC The number of command line arguments

G Argind The argv identifier of the file currently being processed

N ARGV array of command line arguments

G CONVFMT Digital Conversion format%.6g

P ENVIRON UNIX Environment variables

N ERRNO UNIX System error messages

More Wonderful content: http://www.bianceng.cn/database/Oracle/

G fieldwidths A white space-delimited string of input field widths

A FILENAME The name of the current input file

P FNR current record number

A FS input Field separator space

G IGNORECASE Control Case Sensitive 0 (case sensitive)

A NF number of fields in the current record

A NR has been read out of the number of records

The output format of a ofmt number%.6g

A OFS output Field separator space

A ORS output Records separated Shing

A RS input Record He's separated Shing

N Rstart The string first matched by the matching function

N rlength The string length matched by the matching function

N subsep Subscript Separator "\034"

6.awk of built-in functions

V function purpose or return value

------------------------------------------------

N gsub (Reg,string,target) replaces string in target each time a regular expression reg matches

N Index (search,string) returns the position of the search string in string

A Length (string) Number of characters in string string

N Match (String,reg) returns the position of a regular expression reg-matched string

N printf (format,variable) format output, output variable variable in format provided by format.

N-Split (String,store,delim) decomposes a string into an array element of the store based on the delim of the delimiter

N sprintf (format,variable) returns a formatted data containing format, variables is the data to be placed in a string

G strftime (Format,timestamp) returns a date or time series based on format, TIMESTMP is the time returned by the Systime () function

N Sub (reg,string,target) The first time a regular expression reg matches, replace the string in the target string

A substr (String,position,len) returns a substring of Len characters starting at position

P totower (String) returns the corresponding lowercase characters in a string

P ToUpper (String) returns the corresponding uppercase characters in a string

Cotangent (radians) of a atan (x,y) x

Cosine of N cos (x) x (radians)

x power of a exp (x) E

Integer part of a int (x) x

The natural pair value of a log (x) x

N the random number between rand () 0-1

Sine (radians) of N sin (x) x

The square root of a sqrt (x) x

A Srand (x) initializes the random number generator. If x is omitted, the system () is used

G System () returns the time elapsed since January 1, 1970 (in seconds)

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.