awk loop example: awk for and while

Conditional judgment and loop statements are what we must master in development.

Similarly, when we use the linux awk command, we grasp the awk if else conditional judgment statement and awk for, awk while loop statement can help us solve problems quickly.

In the previous article, we introduced in detail the use examples of awk if else statements and awk multiple nested if else statements.


In this article we will share with you examples of the use of awk loop statements for and while.

awk for loop

In the following example, we will share with you a real example of the use of linux awk for loop statement, and will explain the meaning of the command statement line by line to help you fully grasp the awk for loop statement.

First, let’s execute the following command to see what we can get.

➜ awk -F"#" '{
        for(i=1;i<=NF;i++) {
                if($i == "google") {
                        print "NF=" i ", this is a refer";
                } else {
                        print "NF=" i "," $i
                }
        }
}' test-2.txt

The content of test-2.txt is:

192.168.1.1#10.10.83.92#/url/test#google#1234322344

The result of the above command is this:

NF=1,192.168.1.1
NF=2,10.10.83.92
NF=3,/url/test
NF=4, this is a refer
NF=5,1234322344
linux awk for loop

So how is the above awk command executed? Let’s look at it line by line.

1. awk -F”#”

Cut the contents of the file test.txt according to "#", that is:

field 1: 192.168.1.1
field 2: 10.10.83.92
field 3: /url/test
field 4: google
field 5: 1234322344

2. for(i=1;i<NF;i++)

NF: number of fields in the current record.
$0: complete input record.

Because $0 in the linux awk command represents a complete input record, the number of fields counts from 1.

The for loop above means looping from 1 to NF-1.

3. if($i == “google”) {print “NF=” i “, this is a refer”;}

$i: which represents the value of the i-th field.

Here is to check which field's value is "google", and if the field value is google, output the string.

4. else { print $i}

if the field value is not google, output its value.

Ok, the above command has already been introduced.

However, in some cases we also need to use awk for to process some arrays. How do we do this?

awk for loop array

➜ awk 'BEGIN{
        a["k-my"] = "my";
        a["k-name"] = "name";
        a["k-is"] = "is";
        a["k-ylspirit"] = "ylsprit";

        for(i in a) {
                print i, ": " a[i]
        }
}'
for(i in a) 

i: represents the key of array a.
a[i]: represents the value of array a.

awk while loop

In the following example, we briefly introduce the use example of linux awk while loop statement, and the key points that are easy to confuse.

➜  awk -F"#" '{
	i = 0;

	while(i<=NF) { 
		print i ": "$i; 
		i++;
	}
}' test-2.txt

Linux awk while loop, i starts at 0, increments by 1 each time, until i equals the number of fields NF exits the loop.

Reference:


https://www.gnu.org/software/gawk/

One Comment

Add a Comment

Your email address will not be published.