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~~~
|