Functions in awk Using Tutorials

  
 Since awk is a scripting language, there are no complex data type definitions in high-level languages. When defining variables in awk, each variable has a string type value and a numeric type value. Therefore, the functions involved in awk are nothing more than arithmetic operations, string processing, and user-defined functions. The following article will be expanded according to the following three parts:
  • Arithmetic Functions
  • String Functions
  • Custom Functions

    Starting on the journey .
    Arithmetic Functions
    Arithmetic functions basically accept numeric parameters and return numeric types. Commonly used arithmetic functions are as follows:

    Function Name
    Description
    < Th>Examples
    int(x) Round the decimal down awk ‘{print int(4.5), int(4.1), int(4.6)}’,output: 4 4 4 rand(x) back Random number r, where 0 <=r<1 awk ‘{print rand(), rand(), rand()}’, output: 0.237788 0.291066 0.845814 srand(x) to generate a new seed number for rand(), If no seed number is specified, the time of day is used. This function returns the old seed value. See the following detailed summary.
    Among the above three functions, the random number is the one we use the most. Let's talk about the two functions rand and srand in detail.
    There is such an awk script:
    BEGIN { print rand(); print rand(); srand(); print rand(); print rand();} We save this script as rand.awk.
    awk -f rand.awk Run the first output of the following results:
    0.2377880.2910660.01182260.115346 Run the second output the following results:
    0.2377880.2910660.7794110.897179 you will find that before the two runs The random values ​​of the two outputs are the same, which is where you need to pay attention to using these two functions. If the srand function is not called, awk calls srand with a constant as a parameter before starting the program, similar to srand(2); this causes the program to start from the same seed number each time it runs, resulting in The same random number is output. If we want to output a different random number each time we run the script, the best way is to call the srand function in the BEGIN section.
    String functions
    In any language, the processing of strings is very important, awk is no exception, now look at the string functions in awk:

    Function Name
    Description
    gsub(r, s, t) Replaces all strings matched by the regular expression r with the string s in the string t. Returns the number of replacements. If t is not given, the default $0 index(s, t) returns the position of the substring t in the string s length(s) returns the length of the string s, when no s is given, returns the length of $0 match(s , r) if the regular expression r appears in s, returns the starting position of the occurrence; if r does not appear in s, returns 0 split(s, a, sep) Decomposes the string s using the field separator sep Returns the number of elements in the element of array a. If no sep is given, FS is used. The same way is used for array separation and field separation. sprintf Formats output sub(r, s, t) Replaces the first match of the regular expression r with s in the string t. Returns 1 if successful, otherwise returns 0. If t is not given, the default is $0. substr(s, p, n) returns the substring of the string s starting from position p with a maximum length of n. If n is not given, return the remaining string from p tolower(s) to convert all uppercase characters in string s to lowercase and return a new string, the original string will not be changed toupper(s) will be the character All lowercase characters in string s are converted to uppercase and a new string is returned. The original string is not changed.
    Two string substitution functions are provided in awk: gsub and sub. The difference between the two is that gsub is a global replacement, and sub only replaces the content of the first match.
    Test data: Jelly: 26:12474125874:04713365412:0081245: Jelly{ # Replace the string matching "Jelly" on each line with "JellyThink" if (gJ(/Jelly/, "JellyThink" )) print # Output: JellyThink:26:12474125874:04713365412:0081245:JellyThink # Replace the first string matching "JellyThink" with "Jelly" if (sub(/JellyThink/, "Jelly")) Print # Output: Jelly:26:12474125874:04713365412:0081245:JellyThink # Convert all uppercase characters to lowercase print tolower($0) # Output:jelly:26:12474125874:04713365412:0081245:jellythink # Convert all lowercase characters to uppercase Print toupper($0) # Output: JELLY:26:12474125874:04713365412:0081245:JELLYTHINK # Return "T" The position of the character can only return the position of the character print index($0, "T") # Split $0 And calculate the length of each field, the output is as follows: # [1]=Jelly , Length: 5 # [2]=26 , Length: 2 # [3]=12474125874 , Length: 11 # [4]=04713365412 , Length: 11 # [5]=0081245 , Length: 7 # [6]=JellyThink , Length: 10 n = split($0, Field, ":") for (i=1; i<=n; ++i) { value=sprintf("[%d]=%-12s, length:%d", i, field[i ], length(field[i])); print value } if (location = match($0, reg)) { printf("matched to %s\ ", location, reg in %d) } else { printf ("Sorry, no match to %s\ ", reg) }} Custom function
    Let people do DIY, always exciting, in awk, we can also customize our own The function, defined in awk, is written as follows:
    function name(parameter-list){ statements} where parameter-list is a comma-separated list of arguments that are passed as arguments to the function when the function is called. . Next, use a simple example to illustrate the use of custom functions:
    Test data: HelloWorld# Define function function insert(string, pos, ins){ before = substr(string, 1, pos) after = substr(string , pos + 1) return before ins after}# script body { print insert($0, 5, "JellyThink") print before #output: Hello print after #output: World print $0 #output: HelloWorld} in the main body of the script When we print the values ​​of before and after, we find that it can be output. There is one point to note here.

    Awk, the variables defined in the function, the default is global, and the parameters passed are value passing, that is, even if the value of the passed parameter is modified inside the function, outside the function, The value of the parameter is not changed. This is a bit like Lua.
    Look at this way:
    Test data: HelloWorld# Define function function insert(string, pos, ins, before, after){ before = substr(string, 1, pos) after = substr(string, pos + 1) return before ins after}# script body { print insert($0, 5, "JellyThink") print before #output: <empty> print after #output: <empty> print $0 #output: HelloWorld } Do you understand now? When writing awk functions in the work, you need to pay attention to the following two points:

  • Parameters are value passing
  • The variables defined inside the parameters are also global variables

    Summary
    Summary An article is not easy, but also how to typeset this summary, but also to verify each piece of code in the article, this article and "play awk" this, started writing in early October, Later, I tossed Alibaba Cloud and wasted a lot of time. Fortunately, I finally finished it today. not easy! ! ! Fighting~~~

  • Copyright © Windows knowledge All Rights Reserved