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