The gsub function acts like sub, but it matches throughout the document. The format is as follows:
gsub (regular expression, substitution string)
gsub (regular expression, substitution string, target string)
Example:
$ awk '{ gsub(/test/, "mytest"); print }' testfile
$ awk '{ gsub(/test/, "mytest"), $1 }; print }' testfile
The first example matches test throughout the document, and the matching matches are replaced with mytest.
The second example matches in the first domain of the entire document, and all matches are replaced with mytest.
The index function returns the position where the substring was first matched, and the offset starts at position 1. The format is as follows:
index(string, substring)
Example:
$ awk '{ print index("test", "mytest") }' testfile
The instance returns the location of test at mytest, and the result should be 3.
The length function returns the number of characters recorded. The format is as follows:
length( string )
length
Example:
$ awk '{ print length( "test" ) }'
$ awk '{ print length }' testfile
The first instance returns the length of the test string.
The second instance returns the number of characters in the testfile file.
The substr function returns the substring starting from position 1, and returns the entire string if the specified length exceeds the actual length. The format is as follows:
substr( string, starting position )
substr( string, starting position, length of string )
Example:
$ awk '{ print substr( "hello world", 7,11 ) }'
The above example intercepts the world substring.
The match function returns the index of the regular expression position in the string, and returns 0 if the specified regular expression cannot be found. The match function will set the built-in variable RSTART to the start position of the substring in the string, and RLENGTH to the number of characters to the end of the substring. substr can help these variables intercept strings. The function format is as follows:
match( string, regular expression )
Example:
$ awk '{start=match("this is a test",/[a-z]+$/); print start}'
$ awk '{start=match("this is a test",/[a-z]+$/); print start, RSTART, RLENGTH }'
The first instance prints the start position at which the ends with consecutive lowercase characters, here is 11.
The second instance also prints the RSTART and RLENGTH variables, here are 11(start), 11(RSTART), 4(RLENGTH).
toupper and tolower functions can be used for conversion between string sizes, and this function is only valid in gawk. The format is as follows:
toupper( string )
tolower( string )
Example:
$ awk '{ print toupper("test"), tolower("TEST") }'
The split function can split the string into an array by the given separator. If the delimiter is not provided, it is split by the current FS value. The format is as follows:
split( string, array, field separator )
split( string, array )
Example:
$ awk '{ split( "20:18:00", time, ":" ); print time[2] }'
The above example divides the time into the time array by colon and displays the second array element 18.
14.8.2. Time function
The system function returns the entire number of seconds from January 1, 1970 to the current time (excluding leap years). The format is as follows:
systime()
Example:
$ awk '{ now = systime(); print now }'
The strftime function uses the strftime function in the C library to format the time. The format is as follows:
systime( [format specification][,timestamp] )
Table 3. Date and time format specifier
Format Description
%a Abbreviation of the day of the week (Sun)
%A The complete writing of the day of the week (Sunday)
%b Abbreviation of Month Name (Oct)
%B Complete writing of month name (October)
%c Local date and time
%d Decimal date
%D Date 08/20/99
%e Date, if only one is available, a space will be filled
%H represents the hours in 24-hour format in decimal
%I represents the hours in 12-hour format in decimal
%j What day of the year from January 1
%m Month represented in decimal
%M Minutes in decimal
%p 12-hour notation (AM/PM)
%S Second in decimal
%U Decimal represents the week of the year (Sunday is the beginning of a week)
%w The day of the week represented in decimal (Sunday is 0)
%W Decimal represents the week of the year (Monday is the beginning of a week)
%x Reset local date (08/20/99)
%X Reset local time (12:00:00)
%y The year represented by two digits (99)
%Y Current month
%Z Time Zone (PDT)
%%%%%%%%(%)
Example:
$ awk '{ now=strftime( "%D", systime() ); print now }'
$ awk '{ now=strftime("%m/%d/%y"); print now }'
14.8.3. Built-in mathematical functions
Table 4.
Function name return value
atan2(x,y) y, co-cut in the range of x
cos(x) cosine function
exp(x) Suppose the exponent
int(x) round
log(x) natural logarithm
rand() random number
sin(x) sine
sqrt(x) square root
srand(x) x is the seed of rand() function
int(x) rounding, the process does not round
rand() generates a random number greater than or equal to 0 and less than 1
14.8.4. Custom functions
You can also customize functions in awk, with the format as follows:
function name ( parameter, parameter, parameter, ... ) {
statements
return expression # the return statement and expression are optional
}
15. How-to
How to convert a row of vertical rows into horizontal rows?
awk '{printf("%s,",$1)}' filename
Full version
Previous page123Read the full text