Learning awk (3)-function expressions

Source: Internet
Author: User
Tags mathematical functions natural logarithm posix square root

Control statements

Control statement

The control statements are as follows:

If (condition) statement [else statement]
While (condition) Statement
Do statement while (condition)
For (expr1; expr2; expr3) Statement
For (VAR in array) Statement
Break
Continue
Delete array [Index]
Delete Array
Exit [expression]

{Statements}

The above are some loop and control statements, convention, square brackets indicate optional.

I/O statements

I/O expression

The input/output statements are as follows:

Close (file [, how])

Close file, pipe or co-process. The optional how shoshould only be used when closing one end

A two-way pipe to a co-process. It must be a string value, either "to" or "from ".

Closes a file. Optional operations can only be used for media transcoding.

Getline set $0 from next input record; Set NF, NR, FNR.

Set $0 to the value of the next data row. At the same time, NF (number of current data rows and columns), NR (counter -- number of records currently input ),

The value of FNR (the counter of the input file) will also be reset.

Getline <File

Set $0 from next record of file; Set NF.

Set $0 to the value of the next row and NF (number of columns currently logged)

Getline var set var from next input record; Set NR, FNR.
Use the next record to set the variable VAR, and reset NR (number of records currently input) and FNR (number of records in the input file ).

Getline var <File

Set var from next record of file.

Command | Getline [Var]
Run Command piping the output either into $0 or var, as abve.
Use the pipeline's Getline

Command | & Getline [Var]
Run Command as a co-process piping the output either into $0 or var, as abve. Co-Processes
Are a gawk extension.
Mark ...... Urgent Solution

Next stop processing the current input record. The next input record is read and Processing
Starts over with the first pattern in the awk program. If the end of the input data is

Reached, the end block (s), if any, are executed.

Note that this will trigger the logic of the end block when the last line is reached.

Nextfile stop processing the current input file. The next input record read comes from the next Input
File. filename and argind are updated, FNR is reset to 1, and processing starts over
The first pattern in the awk program. If the end of the input data is reached, the end

Block (s), if any, are executed.
Same as above.

Print prints the current record. The output record is terminated with the value of the ORs vari-
Able.
The output ends with the input Terminator.

Print expr-list
Prints expressions. Each expression is separated by the value of the OFS variable. The output
Record is terminated with the value of the ORs variable.
The default delimiter between output expressions is space.

Print expr-List> File
Prints expressions on file. Each expression is separated by the value of the OFS variable.
The output record is terminated with the value of the ORs variable.
Output redirection

Printf FMT, expr-list
Format and print.
Ubiquitous printf ......

Printf FMT, expr-List> File
Format and print on file.
......

System (CMD-line)
Execute the command cmd-line, and return the exit status. (this may not be available on non-
POSIX systems .)
Yes, POSIX

Fflush ()
Flush any buffers associated with the Open output file or pipe file. If file is missing,
Then standard output is flushed. If file is the Null String, then all open output files and
Pipes have their buffers flushed.
By default, the standard output toilet is flushed.

Additional output redirections ctions are allowed for print and printf.

Print...> File

Appends output to the file.

Print... | command
Writes on a pipe.

Print... | & command
Sends data to a co-process.

The Getline command returns 0 on end of file and-1 on an error. Upon an error, errno contains a string describing
The problem.

NOTE: If using a pipe or co-process to Getline, or from print or printf within a loop, you must use close () to re-
Ate new instances of the command. awk does not automatically close pipes or co-processes when they return EOF.

Special file names
Special file name

When doing I/O redirection from either print or printf into a file, or via Getline from a file, gawk recognizes
Certain special filenames internally. These filenames allow access to open file descriptors inherited from gawk's
Parent process (usually the shell). These file names may also be used on the command line to name data files.
Filenames are:

/Dev/stdin the standard input.

/Dev/stdout the standard output.

/Dev/stderr the standard error output.

/Dev/FD/n the file associated with the open file descriptor n.
Open the file identified by the corresponding file descriptor.

These are special useful for error messages. For example:

Print "you blew it! ">"/Dev/stderr"

Whereas you wowould otherwise have to use

Print "you blew it! "|" Cat 1> & 2"
I personally think this cool is more in line with the hobby of programmers.

The following special filenames may be used with the | & Co-process operator for creating TCP/IP network connec-
Tions.

/Inet/tcp/lport/rHost/rport
File for TCP/IP connection on local port lport to remote host rHost on remote port rport. Use a port of 0

Have the system pick a port.
If you use 0, the system will help you select a port for a file linked by TCP/IP protocol.

/Inet/udp/lport/rHost/rport
Similar, but use UDP/IP instead of TCP/IP.
Similarly, this is the UDP version.

/Inet/raw/lport/rHost/rport
Reserved for future use. retained

Other special filenames provide access to information about the running gawk process. These filenames are now
Obsolete. Use the procinfo array to obtain the information they provide. The filenames are:

/Dev/PID reading this file returns the process ID of the current process, in decimal, terminated with a newline.

/Dev/ppid reading this file returns the parent process ID of the current process, in decimal, terminated with

Newline.

/Dev/user reading this file returns a single record terminated with a newline. The fields are separated
Spaces. $1 is the value of the getuid (2) System Call, $2 is the value of the geteuid (2) system call,
$3 is the value of the getgid (2) system call, and $4 is the value of the getegid (2) system call. If
There are any additional fields, they are the group IDs returned by getgroups (2). Multiple groups may
Not be supported on all systems.

Numeric Functions

Numeric functions | mathematical functions

Awk has the following built-in arithmetic functions:

Atan2 (Y, x) returns the arctangent of Y/X in radians.
Returns the arc tangent radians.

Cos (expr) returns the cosine of expr, which is in radians.
The unit of expr should be radians.

Exp (expr) the exponential function
Exponential function.

INT (expr) truncates to integer.
Same as C, different C ++

Log (expr) the natural logarithm function.

Rand () returns a random number N, between 0 and 1, such that 0 <n <1.
Returns a random number ranging from 0 to 1.

Sin (expr) returns the sine of expr, which is in radians.

SQRT (expr) the square root function.

Srand ([expr]) uses expr as a new seed for the random number generator. If no expr is provided, the time of day is
Used. The return value is the previous seed for the random number generator.
Use a random number generation expression. By default, a random number is generated by time.

String Functions

String Functions

Gawk has the following built-in string functions:

Asort (s [, d]) returns the number of elements in the source array S. The contents of S are sorted using
Gawk <A1> <af> S normal rules for comparing values, and the indexes of the sorted values of S
Are
Replaced with sequential integers starting with 1. If the optional destination array D is
Specified, then s is first duplicated into D, and then D is sorted, leaving the indexes
The source array s unchanged.

Asorti (s [, d]) returns the number of elements in the source array S. The behavior is the same as that
Asort (), doesn't that the array indices are used for sorting, not the array values. When
Done, the array is indexed numerically, and the values are those of the original indices.
The original values are lost; thus provide a second array if you want to preserve the orig-
Inal.

Gensub (R, S, H [, T]) search the target string t for matches of the regular expression R. If H is a string
Beginning with G or G, then replace all matches of R with S. Otherwise, H is a number
Indicating which match of R to replace. If t is not supplied, $0 is used instead.
The replacement text S, the sequence \ n, where n is a digit from 1 to 9, may be used
Indicate just the text that matched the n <A1> <af> TH parenthesized subexpression. The sequence
\ 0
Represents the entire matched text, as does the character &. Unlike sub () and gsub (),
Modified string is returned as the result of the function, and the original target string
Is not changed.

Gsub (r, s [, T]) for each substring matching the regular expression R in the string T, substitute the string
S, and return the number of substitutions. If t is not supplied, use $0. An & in
Replacement text is replaced with the text that was actually matched. Use \ & to get a previous-
Eral &. (This must be typed as "\\&"; see gawk: Valid tive awk programming for a fuller
Discussion of the rules for & <A1> <AF <A1> <af> S and backslashes In the replacement text of sub
(), Gsub (),
And gensub ().)

Index (S, T) returns the index of the string T in the string S, or 0 if t is not present. (This implies
That character indices start at one .)

Length ([s]) returns the length of the string S, or the length of $0 if S is not supplied.

Match (S, R [, A]) returns the position in s where the regular expression r occurs, or 0 if R is not present,
And sets the values of rstart and rlength. Note that the argument order is the same as
The ~ Operator: str ~ Re. If array a is provided, A is cleared and then elements 1 through
N are filled with the portions of s that match the corresponding parenthesized subexpres-
Sion in R. The 0 <A1> <af> TH element of A contains the portion of S matched by the entire Reg
Ular
Expression R. subscripts A [n, "Start"], and a [n, "length"] provide the starting index in
The string and length respectively, of each matching substring.

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.