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 linuxShell script to pass an array to awk for processing

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

Share

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

How to use linuxShell script to pass an array to awk for processing, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.

How does the Shell script pass an array to the awk for processing?

Test environment:

[root] # head-1 / etc/redhat-releaseRed Hat Enterprise Linux Server release 6.5 (Santiago) [root] # awk-- version | head-1GNU Awk 3.1.7

As we all know, it is very simple to pass a normal variable to awk in a Shell script, simply assigning a value with the-v argument.

Str1= "Hello World" awk-v str2= "$str1" 'BEGIN {print str2}'

However, it's not that easy to pass an array to awk. Take a look at the following three experiments:

1. Simple arrays can be assigned first and then split

Arr1= (A B C) awk-v arr2= "${arr1 [*]}" 'BEGIN {split (arr2,arr3, "); print arr3 [2]}'

two。 In some cases, it is difficult to find the right delimiter for split, because an array element may contain the character you want to use as the delimiter, and split cannot get the desired result later. So this method is not rigorous enough, especially when we can't predict which characters an array element might contain.

Arr1= (A "B C" D) awk-v arr2= "${arr1 [*]}" 'BEGIN {split (arr2,arr3, "); print arr3 [2]}'

3. You can do this with the help of the export command and awk's ENVIRON default array

Arr1= (A "B C" D) 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.

Share To

Development

Wechat

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

12
Report