In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
Today, I will talk to you about how to use the awk array in the Linux system, many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
1.awk array description
In other programming languages, the subscript of an array starts at 0, that is, if you want to backreference the first element in the array, you need to reference the corresponding subscript [0]. In awk, arrays are also done by referencing subscripts, but in awk, the subscript of an array starts at 1. In other languages, you may be used to "declare" an array first, but not in awk. You can directly assign values to the elements of the array (in fact, if you assign values to the array yourself, it doesn't matter if the subscript starts from 1 or 0! )
two。 When declaring an array, there may be a lot of values, and the command is too long, which reduces the readability of the command, so use the backslash "\" to enter the line feed, and the effect is exactly the same. The code is as follows:
[zkpk@master as] $awk 'BEGIN {arr [1] = "one"; arr [2] = "two"; arr [3] = "three"\; arr [4] = "four"; print arr [3]}' three [zkpk@master as] $awk 'BEGIN {arr [1] = "one"; arr [2] = "two"; arr [3] = "three"; arr [4] = "four"; print arr [3]}' three "
3. The element of the array is set to empty, which is allowed. When there is no element in the array and references it directly, it is assigned to be empty by default, so to judge whether an element exists or not, we cannot use the method that the value of the array element is empty. Instead, we should use the following method:
[zkpk@master as] $awk 'BEGIN {arr [1] = "one"; arr [2] = "two"; arr [3] = "three"; if (4 in arr) {print "four in this arr"}}' [zkpk@master as] $awk 'BEGIN {arr [1] = "one"; arr [2] = "two"; arr [3] = "three"; if (3 in arr) {print "three in this arr"}}' three in this arr
You can also take the opposite approach (using operators! )
[zkpk@master as] $awk 'BEGIN {arr [1] = "one"; arr [2] = "two"; arr [3] = "three"; if (! (4 in arr)) {print "four not in this arr"}}' four not in this arr
4.awk array subscript
In awk, the subscript of an array can be not only a "number", but also an "arbitrary string". In fact, an array in awk is an "associative array". The reason why we first use numbers as an example is to facilitate the habit of having a good transition. However, using numbers as the subscript of an array has certain advantages in some scenarios, but it is also an "associative array" in essence. Awk converts a "number" subscript to a "string" by default, so it is essentially an "associative array" that uses a string as a subscript.
5. Delete array elements
You can delete elements in an array using delete, or you can delete an entire array using delete
[zkpk@master as] $awk 'BEGIN {arr [1] = "one"; arr [2] = "two"; arr [3] = "three";\ > delete arr [1]; print arr [1]}' _ _ (empty) [zkpk@master as] $awk 'BEGIN {arr [1] = "one"; arr [2] = "two"; arr [3] = "three";\ print arr [1]; print arr [3]; delete arr;print arr [1]}' onethree____ (empty)
6. Iterate through the array using for
Syntax: for (variable in array name) {code statement}
Note: where the variable loop is the subscript of the array
[zkpk@master as] $awk 'BEGIN {arr [1] = "one"; arr [2] = "two"; arr [3] = "three"; arr [4] = "four";\ > for (i in arr) {print arr [I]}}' fouronetwothree# unordered print array elements, further proving that they are "associative array" # ordered print array elements [zkpk@master as] $awk 'BEGIN {arr [1] = "one"; arr [2] = "two" Arr [3] = "three"; arr [4] = "four";\ for
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.