Awk built-in Function Analysis and awk built-in functions

Source: Internet
Author: User
Tags mathematical functions natural logarithm uppercase character

Awk built-in Function Analysis and awk built-in functions
I. mathematical functions

The following arithmetic functions perform the same operations as child routines with the same name in C:

Function Name Description
Atan2 (y, x) Returns the arc tangent of y/x.
Cos (x) Returns the cosine of x. x is a radian.
Sin (x) Returns the sine of x, which is a radian.
Exp (x) Returns x power function.
Log (x) Returns the natural logarithm of x.
Sqrt (x) Returns the square root of x.
Int (x) Returns the value of x truncated to an integer.
Rand () Returns any number n, where 0 <=n <1.
Srand ([Expr]) Set the seed value of the rand function to the value of the Expr parameter, or use the time of a certain day if the Expr parameter is omitted. Returns the previous seed value.
Example 1:

[Root @ 361way ~] # Awk 'in in {OFMT = "%. 3f "; fs = sin (1); fe = exp (10); fl = log (10); fi = int (3.1415); print fs, fe, fl, fi ;}'

0.841 22026.466 2.303 3

In the previous built-in function, we have summarized that OFMT sets the output data format to retain three decimal places.

Example 2: obtain a random number

[Root @ 361way ~] # Awk 'in in {srand (); fr = int (100 * rand (); print fr ;}'

[Root @ 361way ~] # Awk 'in in {srand (); fr = int (100 * rand (); print fr ;}'

The above mathematical functions are used less frequently in maintenance, and there are more random numbers. However, it is too complicated to use awk to generate RANDOM numbers. I still use shell variables to generate RANDOM numbers through echo $ RANDOM.

Ii. String Functions
Function Description
Gsub (Ere, Repl, [In]) Except that all the specific values of the regular expression are replaced, it is executed exactly the same as that of the sub function ,.
Sub (Ere, Repl, [In]) Replace 'El' In the string specified by 'in' with the string specified by 'repl'
The first specific value of the extended regular expression specified by the parameter. The number of replicas returned by the sub function. The & (and symbol) that appears In the string specified by the Repl parameter is composed
Replace the string specified by the parameter with the extension regular expression specified by the Ere parameter. If the In parameter is not specified, the default value is the entire record ($0 record variable ).
Index (String1, String2) In the string specified by the String1 parameter (which contains the parameter specified by String2), the return position starts from 1. If the String2 parameter does not exist in the String1 parameter, 0 (zero) is returned ).
Length [(String)] Returns the length (in character format) of the String specified by the String parameter ). If the String parameter is not provided, the length of the entire record is returned ($0 record variable ).
Blength [(String)] Returns the length (in bytes) of the String specified by the String parameter ). If the String parameter is not provided, the length of the entire record is returned ($0 record variable ).
Substr (String, M, [N]) Returns the number of characters specified by the N parameter. Substring from String
The string specified by the parameter is obtained. Its character starts at the position specified by the M parameter. The M parameter specifies the first character in the String parameter as the number 1. If N is not specified
The length of the substring is the length from the position specified by the M parameter to the end of the String parameter.
Match (String, Ere) In the String parameter specified by the String (Ere
In the expanded regular expression specified by the parameter), the return position (character form) starts from 1, or 0 (zero) is returned if the Ere parameter does not appear ). RSTART
Special variables are set to return values. The RLENGTH special variable is set to the length of the matched string, or if no match is found, it is set to-1 (negative one ).
Split (String, A, [Ere]) Splits the parameter specified by the String parameter into array elements A [1], A [2],...
., A [n], and return the value of the n variable. This separator can be performed using the extended regular expression specified by the Ere parameter, or using the current field separator (FS
(If the Ere parameter is not given ). Unless the context specifies that A specific element should also have A numeric value, the elements in array A are created using string values.
Tolower (String) Returns the String specified by the String parameter. Each uppercase character in the String is changed to lowercase. The ing between upper and lower case is defined by the LC_CTYPE category of the current language environment.
Toupper (String) Returns the String specified by the String parameter. Each lowercase character in the String is changed to uppercase. The ing between upper and lower case is defined by the LC_CTYPE category of the current language environment.
Sprintf (Format, Expr, Expr ,...) Format the expression specified by the Expr parameter based on the printf subroutine Format string specified by the Format parameter and return the final generated string.

Note: The Ere part can be a regular expression.

1. Use gsub and sub

[Root @ 361way ~] # Awk 'in in {info = "this is a test2012test! "; Gsub (/[0-9] +/," | ", info); print info }'

This is a test | test!

Find the Regular Expression in info that meets the requirements./[0-9] +/replace it with "|" and assign the value to info and print the output.

2. Search for a string (used for index)

[Root @ 361way ~] # Awk 'in in {info = "this is a test2012test! "; Print index (info," 11111 ")? "OK": "no found ";}'

No found

[Root @ 361way ~] # Awk 'in in {info = "this is a test2012test! "; Print index (info," is ")? "OK": "no found ";}'

OK

[Root @ 361way ~] # Awk 'in in {info = "this is a test2012test! "; Print index (info," test ")? "OK": "no found ";}'

OK

This is cool X, using the ternary operator "expression? Action 1: Action 2 ".

3. Regular Expression matching (used by match)

[Root @ 361way ~] # Awk 'in in {info = "this is a test2012test! "; Print match (info,/[0-9] + /)? "OK": "no found ";}'

OK

4. truncate a string (used by substr)

[Root @ 361way ~] # Awk 'in in {info = "this is a test2012test! "; Print substr (info, 4, 10 );}'

S is a tes

It can be a string of 10 characters starting with 4th characters.

5. String splitting (used for split)

[Root @ 361way ~] # Awk 'in in {info = "this is a test"; split (info, tA, ""); print length (tA); for (k in tA) {print k, tA [k] ;}}'

4

4 test

1 this

2 is

3

Split info, and split the info string into dynamic array tA using spaces. Note awk... An in loop is an unordered loop. It is not from the array subscript 1... N, so pay special attention to it.

6. format the string output (used by sprintf)

The formatting string consists of two parts: some are normal characters, which will be output as is; the other part is the formatting rule character, starting with "%", followed by one or several specified characters, used to determine the output content format. Note that no line breaks are generated by default when printf is used, and the print function adds a line break "\ n" after each line by default.

Format character Description
% D Decimal signed integer
% U Unsigned decimal integer
% F Floating Point Number
% S String
% C Single Character
% P Pointer Value
% E Exponential floating point number
% X % X unsigned hexadecimal integer
% O Unsigned integer in octal format
% G Automatically select the appropriate representation

[Root @ 361way ~] # Awk 'in in {n1 = 124.113; n2 =-1.224; n3 = 1.2345; printf ("%. 2f, %. 2u, %. 2g, % X, % o \ n ", n1, n2, n3, n1, n1 );}'

124.11, 18446744073709551615, 1.2, 7C, 174

[Root @ 361way ~] # Awk 'in in {n1 = 124.113; n2 =-1.224; n3 = 1.2645; printf ("%. 2f, %. 2u, %. 2g, % X, % o \ n ", n1, n2, n3, n1, n1 );}'

124.11, 18446744073709551615, 1.3, 7C, 174

Note: if you look at the above n3 output value, you will find that when using printf for processing, a smart function can be used to rounding up and retaining the decimal point.

Iii. Time Functions
Function Name Description
Mktime (yyyy mm dd hh mm ss [DST]) Generation Time Format
Strftime ([format [, timestamp]) Format the time output and convert the timestamp into a time string.
The specific format is shown in the table below.
Systime () Returns the total number of seconds from January 1, January 1, 1970 to the current time (excluding the leap year ).
1. Obtain the current time of the system using javasime.

The returned timestamp is the number of seconds after the current time minus the 1970-1-1 time.

[Root @ 361way ~] # Awk 'in in {now = require IME (); print now }'

1343210982

2. strftime returns the system time

[Root @ 361way ~] # Awk 'in in {tstamp = 1343210982; print strftime ("% c", tstamp );}'

Wed 25 Jul 2012 06:09:42 CST

[Root @ 361way ~] # Awk 'in in {tstamp = mktime ("2001 01 18 12 15 40"); print strftime ("% c", tstamp );}'

Thu 18 Jan 2001 12:15:40 CST

3. mktime acquisition time difference

[Root @ 361way ~] # Awk 'in in {tstamp1 = mktime ("2001 01 01 12 12 12"); tstamp2 = mktime ("2001 02 01 0 0 0"); print tstamp2-tstamp1 ;}'

2634468

[Root @ 361way ~] # Awk 'in in {tstamp1 = mktime ("2001 01 01 12 12 12"); tstamp2 = require IME (); print tstamp2-tstamp1 ;}'

308201392

First, convert the time to the timestamp through mktime, and then calculate the difference between the two timestamps to obtain the specific difference in seconds between the two timestamps.

Strftime date and time format specifier

Format Description
% Sun)
% Sunday)
% B Abbreviated monthly name (Oct)
% B October)
% C Local Date and Time
% D Decimal date
% D The date is 08/20/99.
% E Date. If only one digit is filled with a space
% H The hour in the 24-hour format in decimal format
% I 12 hours in decimal format
% J The day of the year from January 1, January 1
% M Month in decimal format
% M Minute in decimal format
% P 12-hour representation (AM/PM)
% S Seconds in decimal format
% U The day of the year in decimal format (Sunday is the start of a week)
% W The day of the week in decimal format (Sunday is 0)
% W Day of the year in decimal format (Monday is the start of a week)
% X Reset local date (08/20/99)
% X Reset local time (12:00:00)
% Y Two-digit year (99)
% Y Current month
% Z Time zone (PDT)
% Percent (%)
4. Other functions
Function Description
Close (Expression) Use the Expression parameter with the same string value to disable
Files or pipelines opened by the printf statement or opened by calling the getline function. If the file or MPs queue is successfully closed, the system returns
0; otherwise, a non-zero value is returned. If you want to write a file and read the file later in the same program, the close statement is required.
System (Command) Run the Command specified by the Command parameter and return the exit status. It is equivalent to the system subroutine.
Expression | getline [Variable] Read an input record from the output of the command specified by the Expression parameter transmitted through the pipeline, and specify the value of this record to the Variable specified by the Variable parameter. If the stream that uses the value of Expression as its command name is not enabled currently, a stream is created. The created stream is equivalent to calling the popen subroutine. At this time, the Command parameter takes the value of Expression and the Mode parameter is set to a value of r. As long as the stream is kept open and
If the Expression parameter returns the same string, another record is read for each subsequent call to the getline function. $0 if the Variable parameter is not specified
Record variables and NF special variables are set to records read from the stream.
Getline [Variable] <Expression Read the next input record from the file specified by the Expression parameter and
The Variable specified by the Variable parameter is set to the value of the record. If the stream is retained and the Expression parameter evaluates the value of the same string
Each subsequent call to the function reads another record. If the Variable parameter is not specified, the $0 record Variable and the NF special Variable are set to the record read from the stream.
Getline [Variable] Set the Variable specified by the Variable parameter to the next input record read from the current input file. If the Variable parameter is not specified, the $0 record Variable is set to the value of the record, and the NF, NR, and FNR special variables are also set.
1. Open and close external files (close related)

[Root @ 361way ~] # Awk 'in in {while ("cat/etc/passwd" | getline) {print $0 ;}; close ("/etc/passwd ");}'

Root: x: 0: 0: root:/bin/bash

Bin: x: 1: 1: bin:/sbin/nologin

Daemon: x: 2: 2: daemon:/sbin/nologin

Adm: x: 3: 4: adm:/var/adm:/sbin/nologin

Lp: x: 4: 7: lp:/var/spool/lpd:/sbin/nologin

Sync: x: 5: 0: sync:/sbin:/bin/sync

............ Omitted

2. Read external files line by line (getline usage)

[Root @ 361way ~] # Awk 'in in {while (getline <"/etc/passwd") {print $0 ;}; close ("/etc/passwd ");}'

Root: x: 0: 0: root:/bin/bash

Bin: x: 1: 1: bin:/sbin/nologin

Daemon: x: 2: 2: daemon:/sbin/nologin

Adm: x: 3: 4: adm:/var/adm:/sbin/nologin

Lp: x: 4: 7: lp:/var/spool/lpd:/sbin/nologin

............ Omitted

The following is an example of interactive getline:

[Root @ 361way ~] # Awk 'in in {print "Enter your name:"; getline name; print name ;}'

Enter your name:

361way.com

361way.com

3. Call external applications (system usage)

[Root @ 361way ~] # Awk 'in in {B = system ("ls-l"); print B ;}'

Total 4952

-Rw-r -- 1 root 23276 Dec 1 2012 a. py

-Rw-r -- 1 root 4954897 Jan 19 2012 cms.tar.gz

-Rw-r -- 1 root 73 May 26 2012 git. readme

-Rw-r -- 1 root 401 Mar 25 2012 git_up

Here is the built-in function.

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.