Linux awk built-in function instances

Source: Internet
Author: User
Tags arithmetic month name natural logarithm sin sprintf square root string format uppercase character

Awk built-in functions are divided into 4 main types: arithmetic function, string function, time function, General function

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.

Example:

awk ' begin{ofmt= '%.3f, Fs=sin (3.14/2); Fe=exp (1); Fl=log (exp (2)); Fi=int (3.1415); fq=sqrt (+); print FS, Fe, FL, FI, FQ; }‘

Results:

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

Random number:

awk ' Begin{srand (); Fr=int (100*rand ()); print fr;} '

Results:

64

9

25

Second, String function

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.

1) Sub, gsub use

awk ' begin{info= ' This was a test in 2013-01-04 "; Sub (/[0-9]+/,"! ", info); print info} ' # Sub

Results:

This was a test in!-01-04

awk ' begin{info= ' This was a test in 2013-01-04 "; Gsub (/[0-9]+/,"! ", info); print info} ' # Gsub

Results:

This was a test in!-!-!

2) Index Lookup

awk ' begin{info= ' This was a test in 2013-01-04 "; Print index (info," test ")?                   "Found": "No Found";} ' # match "Test", print "found; mismatch, print" not Found "

Results:

Found

3) Match Matching

awk ' begin{info= ' This was a test in 2013-01-04 "; Print match (info,/[0-9]+/)?                "Found": "No Found";} ' # match numbers, print "found; mismatch, print" not Found "

Results:

Found

4) substr Sub-string

awk ' begin{info= ' This was a test in 2013-01-04 "; print substr (info, 4, 10);} ' # 4–10 characters, starting from 1 counts

Results:

S is a TES

5) Split Split

awk ' begin{info= ' This was a test in 2013-01-04 "; split (info, TA," "); print "Len:" Length (TA);        For (k in TA) {print k, ta[k];} ' # Split with Space "" To print the array length, and its elements

Results:

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

6) sprintf formatted output

Format string 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

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.

awk ' begin{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);} '

Results:

N1 = 124.11, N2 = 18446744073709551615, N3 = 1.2, N1 = 7C, N1 = 174

Three, 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)

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 (%)

Example

awk ' Begin{tstamp=mktime (""%c "); print strftime (" Tstamp ");} '

Results:

Fri 12:12:12 PM CST

awk ' Begin{tstamp1=mktime ("Tstamp2=mktime 0 0 0"); print tstamp2-tstamp1;} '! '.

Results:

2375268

awk ' Begin{tstamp1=mktime ("Tstamp2=systime"); print tstamp2-tstamp1;} '

Results:

33771

Iv. General functions

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.

Example

1) Close usage

awk ' Begin{while ("cat/etc/passwd" | getline) {print $;}; Close ("/etc/passwd");} ' | Head-n10

Results:

Root:x:0:0:root:/root:/bin/bash
Daemon:x:1:1:daemon:/usr/sbin:/bin/sh
Bin:x:2:2:bin:/bin:/bin/sh
Sys:x:3:3:sys:/dev:/bin/sh
Sync:x:4:65534:sync:/bin:/bin/sync
Games:x:5:60:games:/usr/games:/bin/sh
Man:x:6:12:man:/var/cache/man:/bin/sh
Lp:x:7:7:lp:/var/spool/lpd:/bin/sh
Mail:x:8:8:mail:/var/mail:/bin/sh
News:x:9:9:news:/var/spool/news:/bin/sh

2) Getline Usage

awk ' Begin{while (Getline < "/etc/passwd") {print $;}; Close ("/etc/passwd");} ' | Head-n10

Results:

Root:x:0:0:root:/root:/bin/bash
Daemon:x:1:1:daemon:/usr/sbin:/bin/sh
Bin:x:2:2:bin:/bin:/bin/sh
Sys:x:3:3:sys:/dev:/bin/sh
Sync:x:4:65534:sync:/bin:/bin/sync
Games:x:5:60:games:/usr/games:/bin/sh
Man:x:6:12:man:/var/cache/man:/bin/sh
Lp:x:7:7:lp:/var/spool/lpd:/bin/sh
Mail:x:8:8:mail:/var/mail:/bin/sh
News:x:9:9:news:/var/spool/news:/bin/sh

awk ' Begin{print ' Enter your name: "; Getline name; print name;} '

Results:

Enter Your Name:
Root
Root

3) System Usage

awk ' Begin{b=system ("Ls-al"); print B;} '

Results:

Total 32
Drwxr-xr-x 2 Homer Homer 4096 2013-01-04 20:27.
Drwxr-xr-x 4 Homer Homer 4096 2013-01-04 11:35..
-rw-r--r--1 Homer Homer 1773 2013-01-04 19:54 2013-01-03_output_top800_title_url.log
-rw-r--r--1 Homer Homer 1773 2013-01-04 19:55 2013-01-04_output_top800_title_url.log
-rwxr-xr-x 1 Homer Homer 555 2013-01-04 20:23 catline.sh
-rw-r--r--1 Homer Homer 2013-01-04 20:27 ret.txt
-rw-r--r--1 Homer Homer 2013-01-04 19:58 str2.txt
-rw-r--r--1 Homer Homer 2013-01-04 11:15 str.txt

Linux awk built-in function instances

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.