Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use loops in AWK

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to use loops in AWK, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!

AWK is a language that processes text files and is a powerful text analysis tool that often uses loops when dealing with text.

While cycle

A while loop detects an expression and executes the command if the expression is true. When the expression changes to false, the loop breaks.

#! / bin/awk-fBEGIN {# Loop through 1 to 10 ionome1; while (i print i, "to the second power is", iTuni; I = iTun1;} exit;}

In this simple example, awk prints the square of the integer value placed in the variable I. While (I)

#! / usr/bin/awk-fBEGIN {iLife2; do {print I, "to the second power is", iLifei; I = I + 1} while (I exit;} for loop

There are two kinds of for loops in awk.

A for loop initializes a variable, detects a test expression, executes variable increments, and the loop executes all the time when the result of the expression is true.

#! / bin/awk-fBEGIN {for (iTun1; i print i, "to the second power is", iTuni;} exit;}

Another for loop sets an array variable with a contiguous index and executes a set of commands for each index. In other words, it uses an array to "collect" the results of each command execution.

This example implements a simple version of the Unix command uniq. By adding a series of strings as keys to the array a, increasing the key value when the same key appears again, you can get the number of occurrences of a string (like the-count option of uniq). If you print all the keys in the array, you will get all the strings that have appeared.

Use the demo file colours.txt (the file from the previous article) as an example:

Name color amountapple red 4banana yellow 6raspberry red 99strawberry red 3grape purple 10apple green 8plum purple 2kiwi brown 4potato brown 9pineapple yellow 5

This is the awk version of the simple uniq-c:

#! / usr/bin/awk-fNR! = 1 {a [$2] +} END {for (key in a) {print a [key] "" key}

The third column of the sample data file is the count of entries listed in the first column. You can use an array and a for loop to count the entries in the third column by color.

#! / usr/bin/awk-fBEGIN {FS= "; OFS="\ t "; print (" color\ tsum ");} NR! = 1 {a [$2] + = $3;} END {for (bin a) {print b, a [b]}}

As you can see, you also need to print a list of headers in the BEFORE function (executed only once) before processing the file.

Cycle

Loops are an important part of any programming language, and awk is no exception. With loops you can control how the awk script runs, what information it can count, and how it processes your data. Our next article will discuss switch, continue, and next statements.

The above is all the content of the article "how to use loops in AWK". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report