awk built-in string function __ function

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

first, the arithmetic function:

The following arithmetic functions perform the same operation as a subroutine with the same name in the C language:

The name of the function Description
Atan2 (y, x) Returns the y/x of the tangent.
COS (x) Returns the cosine of x, which is radians.
Sin (x) Returns the sine of x, which is radians.
EXP (x) Returns the X-Power function.
Log (x) Returns the natural logarithm of x.
sqrt (x) Returns the X square root.
int (x) Returns the value of the truncation 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 you omit the Expr argument. Returns the previous seed value.

An example is provided:

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

OFMT Set output data format is to keep 3 decimal places

get random numbers:

[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;} '

Two, String functions are:

Function Description
Gsub (Ere, REPL, [in]) It executes exactly like a sub function, except that all the specific values of the regular expression are substituted.
Sub (Ere, REPL, [in]) Replaces the first concrete value of the extended regular expression specified in the string specified by the in parameter with the string specified by the REPL parameter. The Sub function returns the number of replacements. The & (and symbol) that appears in the string specified by the REPL parameter is replaced by 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 a String2-specified parameter appears, returns the position, numbering from 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 argument. If no String argument is given, the length of the entire record (the $ record variable) is returned.
Blength [(String)] Returns the length, in bytes, of the string specified by the string argument. If no String argument is given, the length of the entire record (the $ record variable) is returned.
substr (String, M, [N]) Returns a substring of the number of characters specified by the N parameter. The substring is obtained from the string specified by the string parameter, and its characters begin at the position specified by the M parameter. The M parameter is specified as the first character in the String argument as number 1. If the N parameter is not specified, the length of the substring is the length specified by the M parameter to the end of the String parameter.
Match (a String, Ere) Returns the position (character form) in the string specified by the string argument (in which the extended regular expression specified by the parameter appears), numbering from 1, or 0 (0) If the Ere argument 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]) Splits the arguments specified by the String parameter into an array element a[1], a[2], ..., a[n], and returns the value of the n variable. This separator can be performed by an extended regular expression specified by the parameter, or by the current field delimiter (FS special variable) (if no Ere parameter is given). 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 argument, in which each uppercase character is changed to lowercase. The mapping of uppercase and lowercase is defined by the LC_CTYPE category of the current locale.
ToUpper (String) Returns the string specified by the string argument, with each lowercase character in the string changed to uppercase. The mapping of uppercase and lowercase is defined by the LC_CTYPE category of the current locale.
sprintf (Format, expr, expr, ...) Formats the expression specified by the Expr argument 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 the regular expression in info,/[0-9]+/with "", and replace the value, assign to info not given info value, default is $

Find string (used by index)

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

Not found, returned 0

Regular expression matching lookup (match used)

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

intercept string (substr used)

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

Intercepts 10 length strings starting with the 4th character

string split (split used)

[Chengmo@centos5 ~]$ awk ' begin{info= ' This is 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 more interesting, awk for ... in loop, is a disorderly loop. It is not subscript 1...n from an array, so you need to be aware of it when you use it.

format string output (sprintf used)

Format string format:

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

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

[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) Closes the file or pipe opened by a print or printf statement or calls the Getline function with the same Expression parameter with a string value. Returns 0 if the file or pipe is closed successfully, otherwise returns a value other than 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 the command specified by the command parameter and returns the exit state. Equivalent to the System subroutine.
Expression | Getline [Variable] Reads an input record from the output of the command specified by the Expression parameter in a piped stream and assigns the record's value to the variable specified by the Variable parameter. The stream is created if the value of the Expression parameter is not currently open as a stream with its command name. The stream created is equivalent to calling the Popen subroutine, at which time the Command parameter takes the value of the Expression parameter and the Mode argument is set to a value of R. Each subsequent call to the Getline function reads another record as long as the stream remains open and the Expression parameter evaluates to the same string. If the Variable parameter is not specified, the $ record variable and the NF special variable are set to the records 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 the stream remains open and the Expression parameter is evaluated for the same string, another record is read for each subsequent invocation of the Getline function. If the Variable parameter is not specified, the $ record variable and the NF special variable are set to the records 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 the Variable parameter is not specified, 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 external files line-by-row (Getline use method)

[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 Returns the value, is the execution result

four, Time function

 

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

Create a specified time (Mktime used)

[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

To find the intermediate time difference of 2 strftime, this paper introduces the method of using

[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 day of the Week (Sun)
%A The full wording of the week (Sunday)
%b Abbreviation for the month name (OCT)
%B The 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 will fill a space
%H Hours in decimal notation for 24-hour format
%I Hours in decimal notation for 12-hour format
%j The first day of the year from January 1
%m The month represented by decimal
%m Minutes in decimal notation
%p 12-hour Representation (AM/PM)
%s Decimal representation of seconds
%u The first weeks of the year in decimal notation (Sunday as the beginning of one weeks)
%w The day of the Week in decimal (Sunday is 0)
%w The first 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 Year of the two-digit number expression (99)
%Y Current month
%Z Time Zone (PDT)
%% Percent semicolon (%)

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.