Awk command advanced, awk advanced

Source: Internet
Author: User
Tags mathematical functions natural logarithm uppercase character

Awk command advanced, awk advanced
Built-in functions
Mathematical functions

Atan2 (y, x) Calculate the arc tangent of y/x
Cos (x) Returns the cosine of x. x is a radian.
Sin (x) Returns the sine of x. x is a radian.
Exp (x) Calculate the x power of e
Log (x) Calculate 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 a random number ranging from 0 ~ 1, less than 1
Srand (x) Set the seed value of the rand function. If the parameter is omitted, the time is used as the parameter. Returns the previous seed value.

Awk 'in in {printexp (1)} '2. 71828awk 'in in {printsqrt (25)} '5awk' BEGIN {printlog (exp (1)} '1awk' BEGIN {printint (5.5)} '5 does not use srand, the Random Number of rand is the same every time awk 'in in {printrand ()} '0. 237788awk 'in in {printrand ()} '0. 237788 after srand is used, awk 'in in {srand (); printrand ()} '0 is different each time. 110823awk 'in in {srand (); printrand ()} '0. 681478

String Functions

Gsub (pattern, des, src) Replace all src strings matched by pattern with des.
Sub (pattern, des, src) Use des to replace the first string matched by pattern in src. The number of replicas returned by the function. If the src parameter is not specified, the default value is the entire record ($0)
Index (str1, Str2) The position where the string in str2 appears in str1 starting from 1. If no value exists, 0 is returned.
Length (str) Returns the length (in character format) of the string specified by the str parameter ). If the str parameter is not given, the length of the entire record is returned ($0)
Substr (str, ptr, n) Returns a string whose length starts from ptr to n in string s. If n is not specified, returns the string at the position specified by ptr to the end of the str parameter.
Match (str, pattern) Returns the starting position of matching pattern in str, and sets the RLENGTH variable to the number of matching results. If no match exists, RLENGTH is-1.
Split (str, array, [pattern]) Splits the str string by the separator pattern and exists in the array. Use the default delimiter when pattern is omitted
Tolower (str) Returns the string specified by the str parameter. Each uppercase character in the string is changed to lowercase.
Toupper (str) Returns the string specified by the str parameter. Each lowercase character in the string is changed to uppercase.
Sprintf (format, expr, expr ,...) Returns the formatted string.

cat tmp.txtstr strstr strawk '{sub(/str/,"123");print $0}' tmp.txt123ing strstr strawk '{gsub(/str/,"123");print $0}' tmp.txt123ing 123ing123ing 123ingawk 'BEGIN{print index("1234","23")}'2awk 'BEGIN{print length("1234")}'4$awk 'BEGIN{print substr("1234",2,2)}'23awk 'BEGIN{print substr("1234",2)}'234awk 'BEGIN{print match("abcd",/cd/);print RLENGTH}'32awk 'BEGIN{print match("abcd",/e/);print RLENGTH}'0$awk 'BEGIN {split("123;456;789",array,/;/);i=0;for(i in array)print array[i]}'123456789awk 'BEGIN {print toupper("string");}'STRINGawk 'BEGIN {print tolower("STRING");}'stringawk 'BEGIN {a=1;b="string";print sprintf("hello %d hello %s",a,b);}'hello 1 hello string

Other functions
Close (filenameORcommand) Close open files or pipelines. If the call succeeds, 0 is returned. Otherwise, a non-zero value is returned.
System (command) Run the command specified by the command parameter and return the exit status. Equivalent to the system subroutine
Expression | getline [variable] The expression command output is read into variable through the pipeline. If the expression pipeline is not opened yet, the pipeline is created and read-only. As long as the pipeline remains open, the next row of records will be read for each call to the getline function. If the variable parameter is not specified, the $0 record variable is set to the value of the record, and the NF, NR, and FNR special variables are also set.
Getline [variable] <filename Read the next record from the file specified by filename and the record is in variable. As long as the input stream remains open and the filename parameter evaluates the same string, the next row of records will be read for each call to the getline function. If the variable parameter is not specified, the $0 record variable is set to the value of the record, and the NF, NR, and FNR special variables are also set.
Getline [variable] Set 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 $0 record variable is set to the value of the record, and the NF, NR, and FNR special variables are also set.
awk 'BEGIN{system("cat tmp.txt")}'string stringstring stringawk 'BEGIN {"cat ~/tmp.txt"  | getline;print $0;close("cat ~/tmp.txt");}'string stringstring stringawk 'BEGIN {getline a< "tmp.txt";print a;close("tmp.txt");}'string stringstring string

Mktime ("yyyy mm dd hh mm ss ") Generation Time Format
Strftime ([format [, timestamp]) Format the time output and convert the timestamp into a time string.
Systime () Returns the total number of seconds from January 1, January 1, 1970 to the current time (excluding the leap year ).

awk 'BEGIN{last=mktime("2012 01 02 03 04 05");print strftime("%Y %m %d %H %M %S",last)}'2012 01 02 03 04 05


Format Description
% Sun)
% Sunday)
% B Abbreviated monthly name (Oct)
% B October)
% C Local Date and Time
% D Decimal date
% D The date is 08/20/99.
% E Date. If only one digit is filled with a space
% H The hour in the 24-hour format in decimal format
% I 12 hours in decimal format
% J The day of the year from January 1, January 1
% M Month in decimal format
% M Minute in decimal format
% S Seconds in decimal format
% U The day of the year in decimal format (Sunday is the start of a week)
% W The day of the week in decimal format (Sunday is 0)
% W Day of the year in decimal format (Monday is the start of a week)
% X Reset local date (08/20/99)
% X Reset local time (12:00:00)
% Y Two-digit year (99)
% Y Year in full number
% Z Time zone (PDT)
% P 12-hour representation (AM/PM)
% Percent (%)


Custom Functions


Similar to C functions, but the parameter type and return value type.

Function definitions start with functions and can be defined anywhere in a non-statement block.

Function call provides zero or more parameters. For missing parameters, the default value is 0 or an empty string.

You can use the return keyword to end the function and return it in advance.

If the parameter of the function is a scalar, it is a value, and the array is a reference. Changing the value of the array in the function can change the value in the global array.

$0, $1, $2... all scripts are used by default. It can be considered global no matter inside or outside the function.
Awk 'in in {print max (1, 2, 3)} function max (a, B, c) {a = a> B? A: B; return a> c? A: c;} '3awk' BEGIN {print max (1, 2, 3)} function max (a, B, c) {a = a> B? A: B; return a; a> c? A: c;} '2awk' BEGIN {print max (1, 2, 3)} function max (a, B, c) {a = a> B? A: B; a> c? A: c;} 'Output Space


Command Line execution method awk [option] 'commands' input file

Shell scripts write all the awk commands in one file and make the awk program executable. Then the awk command interpreter serves as the first line of the script.
Equivalent to the first line of shell script :#! /Bin/sh, which can be changed :#! /Bin/awk-f

Awk-f inserts all awk commands into a separate file, and then calls:
Awk-f awk script input file
-F option: load the awk script.


Include and load


You can reference other awk scripts or library functions in an awk script.

Use the keyword include and load to reference @ include "filename" @ load "filename"


You can also specify include-file through-I, and specify load-lib through-l.

The default search path of the file is specified by AWKPATH and AWKLIBPATH in the environment variable ENVIRON.
awk 'BEGIN{print sprintf("AWKPATH =" ENVIRON["AWKPATH"])}'AWKPATH =.:/usr/share/awkawk 'BEGIN{print sprintf("AWKLIBPATH =" ENVIRON["AWKLIBPATH"])}'AWKLIBPATH =/usr/lib/gawk
In the awksrc file, cat awksrcfunction max (a, B, c) {a = a> B? A: B; return a> c? A: c;} function min (a, B, c) {a = a <B? A: B; return a <c? A: c;} cat testawk. awk #! /Bin/awk-f @ include "awksrc" BEGIN {print "Enter three numbers and the maximum value will be output:"} {print max ($1, $2, $3)}; run :. /testawk. awk input 3 numbers, will output the maximum value: 1 2 33




How to Use awk in shell commands

1. awk '{print $4}' spinout

This command is the fourth column used to extract text. Note the format.
Awk and single quotes must have spaces, followed by a single quotation mark and a file name.

2. If you want to extract the fourth line of text

Awk 'nr = 4 {print} 'spinout

You can also use sed to extract the fourth line.

Sed-n 4 p spinou

How to Use the linux awk command?

Awk: used to split a row into several "fields" for processing. Suitable for processing small data.
Running Mode: awk 'condition type 1 {Action 1} Condition Type 2 {Action 2}... 'filename

# Last | awk '{print $1 "\ t" $3}' <= view registrant's data. Only the logon name and IP address are displayed and separated by [tab ].

Awk built-in Variables
Meanings of variable names

Total number of fields owned by each line of NF ($0)

NR the current awk processes the "nth row" Data

FS current delimiter, default Space key

Logical operators of awk
Meaning of the computing unit
> Greater
<Less
> = Greater than or equal
<= Less than or equal
= Equal
! = Not equal

Example:
Cat/etc/passwd | awk '{FS = ": "} $3 <10 {print $1" \ t "$3} '<= file/etc/passwd is separated, view the data smaller than 10 in the third column, and only the accounts and third columns are displayed.

The above is my summary of awk and I hope it will help you. I wrote it, not just copy it.

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.