Built-in string control functions in awk

Source: Internet
Author: User
Tags month name natural logarithm numeric value sin sprintf square root string format uppercase character
Common built-in functions awk built-in string functions
Gsub (R,s) replaces R with s in the entire $
Gsub (R,S,T) replaces R with S in the whole t
Index (S,T) returns the first position of the string T in S
Length (s) returns s
Match (S,R) tests if s contains a string that matches R
Split (S,A,FS) divides s into sequence a on FS
Sprint (FMT,EXP) returns the EXP formatted by FMT
Sub (r,s) replaces s with the longest leftmost substring in
SUBSTR (s,p) returns the suffix part of the string s starting from P
SUBSTR (s,p,n) returns the suffix part of the string s starting from p in length n

First, arithmetic function:

The following arithmetic function performs the same operation as a subroutine with the same name as the C language:

Name of function Description
Atan2 (y, x) Returns the y/x of the inverse tangent.
COS (x) Returns the cosine of x, and the x is the radian.
Sin (x) Returns the sine of x, and the x is the radian.
EXP (x) Returns the X-Power function.
Log (x) Returns the natural logarithm of x.
sqrt (x) Returns the square root of x.
int (x) Returns the truncated value of x 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 day if the expr parameter is omitted. Returns the previous seed value.

To illustrate:

[Chengmo@centos5 ~]$ awk ' begin{ofmt= '%.3f '; fs=sin (1); Fe=exp (Fl=log) fi=int 3.1415 ();p rint fs,fe,fl,fi;} '
0.841 22026.466 2.303 3

OFMT setting the output data format is reserved for 3 decimal places

get random number:

[Chengmo@centos5 ~]$ awk ' Begin{srand (); Fr=int (100*rand ());p rint fr;} '
78
[Chengmo@centos5 ~]$ awk ' Begin{srand (); Fr=int (100*rand ());p rint fr;} '
31
[Chengmo@centos5 ~]$ awk ' Begin{srand (); Fr=int (100*rand ());p rint fr;} '

the second, string function is:

Function Description
Gsub (Ere, REPL, [in]) In addition to the regular expression all the specific values are substituted for this, and it executes exactly like the sub function.
Sub (Ere, REPL, [in]) Replaces the first concrete value of the extended regular expression specified by the Ere parameter with the string specified by the REPL parameter, in the string designated by the in parameter. The Sub function returns the number of replacements. The & (and symbol) that appears in the string specified by the REPL parameter is replaced with a string specified by the in parameter that matches the specified extended regular expression of the Ere parameter. If the in parameter is not specified, the default value is the entire record (the $ record variable).
Index (STRING1, String2) In the string specified by the String1 parameter, where there is a parameter specified by String2, returns the position, numbering starting at 1. Returns 0 (0) If the String2 parameter is not present in the STRING1 parameter.
length [(String)] Returns the length (in character form) of the string specified by the string parameter. If the String argument is not given, the length of the entire record is returned (the $ record variable).
Blength [(String)] Returns the length, in bytes, of the string specified by the string parameter. If the String argument is not given, the length of the entire record is returned (the $ record variable).
substr (String, M, [N]) Returns a substring with the number of characters specified by the N parameter. The substring is obtained from the string specified by the string parameter, and its character begins at the position specified by the M parameter. The M parameter is specified as the first character in the String parameter as number 1. If the N parameter is not specified, the length of the substring will be the length specified by the M parameter to the end of the String parameter.
Match (String, Ere) Returns the position (in character form) in the string specified by the strings parameter (the extension regular expression specified by the Ere parameter), numbering starting at 1, or 0 (0) If the ere parameter does not appear. The Rstart special variable is set to the return value. The Rlength special variable is set to the length of the matched string, or if no match is found, set to 1 (minus one).
Split (String, A, [Ere]) Divides the parameter specified by the String parameter into the array element a[1], a[2], ..., a[n], and returns the value of the n variable. This separation can be done with an extended regular expression specified by the Ere parameter, or with the current field delimiter (FS special variable) (if the Ere parameter is not given). The elements in a array are created with string values unless the context indicates that a particular element should also have a numeric value.
ToLower (String) Returns the string specified by the string parameter, with each uppercase character in the string changed to lowercase. Uppercase and lowercase mappings are defined by the LC_CTYPE category of the current locale.
ToUpper (String) Returns the string specified by the string parameter, with each lowercase character in the string changed to uppercase. Uppercase and lowercase mappings are defined by the LC_CTYPE category of the current locale.
sprintf (Format, expr, expr, ...) Formats the expression specified by the Expr parameter and returns the last generated string, based on the printf subroutine format string specified by the format parameter.
ere all can be regular expressions

gsub,sub Use

[Chengmo@centos5 ~]$ awk ' begin{info= "This is a test2010test!"; Gsub (/[0-9]+/, "!", info);p rint info} '
This is a test!test!

Find satisfies regular expression in info,/[0-9]+/replace with "", and replace value, assign value to info not give info value, default is $

Find string (index use)

[Wangsl@centos5 ~]$ awk ' begin{info= "This is a test2010test!"; Print index (info, "test")? " OK ":" No Found ";} '
Ok

Not found, return 0

Regular expression matching lookup (match use)

[Wangsl@centos5 ~]$ awk ' begin{info= "This is a test2010test!"; Print match (info,/[0-9]+/)? " OK ":" No Found ";} '
Ok

Intercept string (substr use)

[Wangsl@centos5 ~]$ awk ' begin{info= "This is a test2010test!"; Print substr (info,4,10);} '
S is a TES

Intercept 10 length strings starting with the 4th character

string splitting (split use)

[Chengmo@centos5 ~]$ awk ' begin{info= "This was a Test"; Split (Info,ta, "");p rint length (TA); for (k in TA) {print k,ta[k];}} '
4
4 test
1 This
2 is
3 A

Split info, dynamically create array ta, here is interesting, awk for ... in loop, is an unordered loop. Not from the array subscript 1...N, so use time need to pay attention.

formatted string output (sprintf use)

Format string format:

Where the formatted string consists of two parts: some are normal characters, and the characters are output as-is; The other part is the formatting of the specified character, starting with "%", followed by one or more specified characters, used to determine the output content format.

Format character Description
%d Decimal signed integer
%u Decimal unsigned integer
%f Floating point number
%s String
%c Single character
%p The value of the pointer
%e Floating-point numbers in exponential form
%x %x unsigned integer represented in hexadecimal
%o unsigned integer represented in octal
%g Automatic selection of appropriate representations

[Chengmo@centos5 ~]$ awk ' begin{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

Third, the general function is:

Function Description
Close (Expression) Use the same Expression argument with a string value to close the file or pipeline opened by the print or printf statement or by calling the Getline function. Returns 0 if the file or pipeline is successfully closed, and other values that do not have a value of 0. If you intend to write a file and later read the file in the same program, the close statement is required.
System (Command) Executes commands specified by the command parameter and returns the exit status. is equivalent to the system subroutine.
Expression | Getline [Variable] Reads an input record from the output of the command specified by the Expression parameter through the pipeline, and assigns the value of the record to the variable specified by the Variable parameter. Creates a stream if the value of the Expression parameter is not currently open as a stream of its command name. The created stream is equivalent to calling the Popen subroutine, at which point the Command parameter takes the value of the Expression parameter and the Mode parameter is set to a value of R. Whenever a stream is left open and the Expression argument evaluates to the same string, another record is read for each subsequent call to the Getline function. If the Variable parameter is not specified, the $ record variable and the NF special variable are set to the record read from the stream.
Getline [Variable] < Expression Reads the next record of the input from the file specified by the Expression parameter, and sets the variable specified by the Variable parameter to the value of the record. Whenever a stream is left open and the Expression parameter evaluates to the same string, another record is read for each subsequent call to the Getline function. If the Variable parameter is not specified, the $ record variable and the NF special variable are set to the record read from the stream.
Getline [Variable] Sets the variable specified by the Variable parameter to the next input record read from the current input file. If you do not specify the Variable parameter, the $ record variable is set to the value of the record, and the NF, NR, and FNR special variables are also set.

Open External file (Close usage)

[Chengmo@centos5 ~]$ awk ' begin{while ("cat/etc/passwd" |getline) {print $;}; Close ("/etc/passwd");} '
Root:x:0:0:root:/root:/bin/bash
Bin:x:1:1:bin:/bin:/sbin/nologin
Daemon:x:2:2:daemon:/sbin:/sbin/nologin

read the external file line by row (getline usage)

[Chengmo@centos5 ~]$ awk ' Begin{while (Getline < "/etc/passwd") {print $;}; Close ("/etc/passwd");} '
Root:x:0:0:root:/root:/bin/bash
Bin:x:1:1:bin:/bin:/sbin/nologin
Daemon:x:2:2:daemon:/sbin:/sbin/nologin

[Chengmo@centos5 ~]$ awk ' begin{print ' Enter your name: "; getline name;print name;} '
Enter Your Name:
Chengmo
Chengmo

 

calling external applications (System usage method)

[Chengmo@centos5 ~]$ awk ' Begin{b=system ("Ls-al");p rint b;} '
Total 42092
Drwxr-xr-x Chengmo Chengmo 4096 09-30 17:47.
Drwxr-xr-x-root root 4096 10-08 14:01.

b return value, which is the execution result

four, Time function

 

Name of function Description
Mktime (YYYY mm DD HH mm ss[DST]) Generate Time Format
strftime ([format [, timestamp]]) Format time output, convert timestamp to time string
Specific format, see the table below.
Systime () Gets a timestamp that returns the number of seconds from January 1, 1970 to the current time (excluding leap years)

Create a specified time (mktime use)

[Chengmo@centos5 ~]$ awk ' Begin{tstamp=mktime ("2001");p rint strftime ("%c", Tstamp);} '
January 01, 2001 Monday 12:12 12 seconds

[Chengmo@centos5 ~]$ awk ' Begin{tstamp1=mktime ("2001") Tstamp2=mktime ("2001 0 0 0");p rint tstamp2-tst AMP1;} '
2634468

In order to find the intermediate time difference of 2 period, the method of using Strftime is introduced.

[Chengmo@centos5 ~]$ awk ' Begin{tstamp1=mktime ("2001"); Tstamp2=systime ();p rint tstamp2-tstamp1;} '
308201392

strftime Date and time format specifier

format Description
%a Abbreviation of the Day of the Week (Sun)
%A The full wording of the day of the Week (Sunday)
%b Abbreviation for month name (OCT)
%B Full wording of the month name (October)
%c Local date and time
%d Decimal Date
%d Date 08/20/99
%e Date, if only one would fill a space
%H Hours in decimal for 24-hour format
%I Hours in decimal for 12-hour format
%j The day of the year from January 1 onwards
%m The Month in decimal notation
%M Minutes in decimal notation
%p 12-hour notation (AM/PM)
%s The seconds in decimal notation
%u The first few weeks of the year in decimal notation (Sunday as the beginning of one weeks)
%w The day of the Week in decimal notation (Sunday is 0)
%W The first few weeks of the year in decimal notation (Monday as the beginning of one weeks)
%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 percent (%)

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.