How to use functions and control structures in "awk"
"awk" supports both built-in and user-defined functions. You can call a function by using its name and passing arguments in parentheses. For example, to print the length of each line in a file, you can use: awk '{print length($0)}' file The length function returns the number of characters in a string. You can also define your own functions using the function keyword, and return values using the return keyword. For example, to create a function that reverses a string, you can write:
function reverse(s) {
r = ""
for (i=length(s); i>0; i--)
r = r substr(s,i,1)
return r
}
The function reverse takes a string s as an argument, and returns a new string r that is the reverse of s. The substr function returns a substring of a string.
Control structures are used to control the flow of execution in "awk". They include if-else, for, while, and break. For example, to print only the lines that contain the word "Linux" in a file, you can use: awk '{if ($0 ~ /Linux/) print $0}' file The if-else statement evaluates a condition and executes a block of code accordingly. The tilde (~) operator matches a regular expression.