Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

Previously introduced the use of awK variables, but with variables can only be passive use, can not be active control, that how silly ah.

So awK also provides syntax for controlling program logic, such as loop statements and conditional statements.

Awk’s for loop is similar to C

for(i=xx; i<=xxxx; i++){ xxxx }Copy the code

The main difference is the location of variable declarations, or awK doesn’t have any declarations, it just initializes variables, so that’s how it is. When we first wrote shell word frequency statistics, we introduced three ways to convert multiple strings from one line to one string per line. Now that we have the loop, we can do this manually. Awk divides the string for each row by field, and it has a built-in variable NF that represents the number of fields in the current row. This gives us the same effect using a loop that prints each field of each row as a single line.

The code is as follows:

awk '{ for(i=1; i<=NF; i++){ print $i } }'
Copy the code

It is important to note that the split field in AWK starts at the beginning, not zero. The zero field holds the entire row, and the items after 1 are the components. And C language left closed right open style is not the same.

Awk conditional statements are similar, as follows

if (xxx ){
  xxxxxx
}else if(xxx) {
  xxxx
}else{
  xxxx
}
Copy the code

We can start by simply testing the conditional statement, assuming that every third line we draw a dividing line. We can use the variable NR that comes with AWK, which we used earlier to calculate the average. But we only used its value at END. In fact, it is different from NF, where NR should be seen as the number of rows currently processed, whereas NF refers only to the number of fields in the current row.

Pretend to be an 18-line file with seq. The code can be written like this

seq 18 | awk '{print $0; if(NR%3==0){print "===================="}}'
Copy the code

From a data processing point of view, conditional branching allows us to implement an important operation in relational algebra: selection.

For example, let’s say we want to select all the people with a passing grade, let’s say we use SEQ instead of file, let’s say we have a passing grade of 6 out of 10.

It could be something like this:

seq 10 | awk '{if($1>=6){print $0}}'
Copy the code

This filters out the rows that meet the criteria.

We can actually complete data filling (or data modification) if we make certain changes to the condition, such as filling in the specified value when the data is null (or not printing or changing the value of incorrect data).

Finally, with these flow-control statements, AWK has the expressiveness of a scripting language, while the C-like syntax keeps the code readable.