Linuxawk built-in function instance

Source: Internet
Author: User
Tags natural logarithm uppercase character
Awk built-in functions, mainly divided into four types: Arithmetic functions, string functions, time functions, general functions

Awk built-in functions, mainly divided into four types: Arithmetic functions, string functions, time functions, general functions

I. arithmetic 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]) sets the seed value of the rand function to the value of the Expr parameter, or uses the time of a certain day if the Expr parameter is omitted. Returns the previous seed value.

Example:

Awk 'In in {OFMT = "%. 3f "; fs = sin (3.14/2); fe = exp (1); fl = log (exp (2); fi = int (3.1415 ); fq = sqrt (100); print fs, fe, fl, fi, fq ;}'

Result:

1.000 2.718 2 3 10 # sin (3.14/2) = 1.000; exp (1) = 2.718; log (exp (2) = 2; int (3.1415) = 3; sqrt (100) = 10

Random number:

Awk 'In in {srand (); fr = int (100 * rand (); print fr ;}'


Result:

64

9

25

II. string functions

Function description
Gsub (Ere, Repl, [In]) is executed In the same way as the sub function except that all the specific values of the regular expression are replaced ,.
Sub (Ere, Repl, [In]) replaces the first specific value of the extended regular expression specified by the Ere parameter In the string specified by the In parameter with the string specified by the Repl parameter. The number of replicas returned by the sub function. The & (and symbol) appearing In the string specified by the Repl parameter is replaced by the string specified by the In parameter that matches the extended 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 (where String2 is specified), return position, starting from 1. If the String2 parameter does not exist in the String1 parameter, 0 (zero) is returned ).
Length [(String)] returns the length (in character form) 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 of the String specified by the String parameter (in bytes ). If the String parameter is not provided, the length of the entire record is returned ($0 record variable ).
Substr (String, M, [N]) returns a quantum String with the number of characters specified by the N parameter. The substring is obtained from the String specified by the String parameter. 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 specified by the String parameter (the extended regular expression specified by the Ere parameter appears in it) returns the position (character form), starting from 1, or if the Ere parameter does not appear, 0 (zero) is returned ). 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 n variable. This separator can be performed using the extended regular expression specified by the Ere parameter or using the current field separator (FS special variable) (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.

1) use sub and gsub


Awk 'In in {info = "this is a test in 2013-01-04"; sub (/[0-9] + /,"! ", Info); print info} '# sub


Result:

This is a test in! -01-04

Awk 'In in {info = "this is a test in 2013-01-04"; gsub (/[0-9] + /,"! ", Info); print info} '# gsub


Result:

This is a test in! -! -!

2) index search

Awk 'In in {info = "this is a test in 2013-01-04"; print index (info, "test ")? "Found": "no found";} '# matches "test" and prints "found". if no matches are found, the "not found" is printed.


Result:

Found

3) match

Awk 'In in {info = "this is a test in 2013-01-04"; print match (info,/[0-9] + /)? "Found": "no found";} '# match the number and print "found; mismatch. print" not found ".


Result:

Found

4) substr substring

Awk 'In in {info = "this is a test in 2013-01-04"; print substr (info, 4, 10);} '#4-10 characters, starting from 1


Result:

S is a tes

5) split

Awk 'In in {info = "this is a test in 2013-01-04"; split (info, tA, ""); print "len:" length (tA ); for (k in tA) {print k, tA [k] ;}' # Separate by space "", print the array length, and each element


Result:

Len: 6
4 test
5 in
6 2013-01-04
1 this
2 is
3

6) sprintf format output

Format the string:


Format Description
% D decimal signed integer
% U decimal unsigned integer
% F floating point number
% S string
% C single character
% P pointer value
% E exponential floating point number
% X % X unsigned integer in hexadecimal format
% O unsigned integer in octal format
% G automatically selects the appropriate notation

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.


Awk 'In in {n1 = 124.113; n2 =-1.224; n3 = 1.2345; printf ("n1 = %. 2f, n2 = %. 2u, n3 = %. 2g, n1 = % X, n1 = % o \ n ", n1, n2, n3, n1, n1 );}'


Result:

N1 = 124.11, n2 = 18446744073709551615, n3 = 1.2, n1 = 7C, n1 = 174

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.
Iime () gets the timestamp and returns the whole number of seconds from January 1, January 1, 1970 to the current time (excluding the leap year ).

Strftime date and time format specifier


Format Description
% A abbreviation of the day of the week (Sun)
% A complete statement of the day of the week (Sunday)
% B abbreviated name (Oct)
% October)
% C local date and time
% D decimal date
% D: date 08/20/99
% E Date. if only one digit is filled with a space
% H represents the hour in the 24-hour format in decimal format.
% I represents the hour in 12 hour format in decimal format
% J the day of the year from January 1, January 1
Month in % m decimal format
% M in decimal format
% P 12-hour notation (AM/PM)
% S in decimal format
% U

Related Article

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.