How to pass an array to awk inside a Linux shell script for processing

Source: Internet
Author: User

Some time ago and several colleagues discussed a question: How does a shell script pass an array inside awk for processing?
No method was found at that time. The previous two days in the QQ group to discuss awk, inadvertently chat about this topic. opportunity to find a train of thought, hereby share.
Test environment:
[root]# head-1/etc/redhat-release
Red Hat Enterprise Linux Server release 6.5 (Santiago)
[root]# awk--version | Head-1
GNU Awk 3.1.7
As we all know, it is very simple to pass an ordinary variable to awk in a shell script, just by assigning a value directly to the-v parameter.
str1= "Hello World"
Awk-v str2= "$str 1" ' Begin{print str2} '
However, it's not that simple to pass an array to awk. Take a look at the following three trials:
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]} '
2. In some cases it is difficult to find a suitable delimiter to split, because an array element may contain the character you want to use as a delimiter, and after split, you cannot get the desired result. So this approach is not rigorous enough, especially if we can't predict which characters the array element might contain.
Arr1= (A "B C" D)
Awk-v arr2= "${arr1[*"} "' Begin{split (ARR2,ARR3," "); Print arr3[2]} '
3. This function can be achieved with the export command and awk's environ default array
Arr1= (A "B C" D)
For ((i=0;i<${#arr1 [*]};i++)]; Do
Export arr1_m$i= "${arr1[$i]}"
Done
awk ' Begin{for (i in ENVIRON) if (i~/arr1_m/) print I "=" environ[i]} '
I'm just here to demonstrate the functionality, so I didn't write the definition of the export variable name and the string match within awk, so we can adjust it according to the actual situation (such as adding more restrictive conditions, etc.).
Conclusion: It is technically possible to pass an array to awk inside the shell script, but it is not recommended to be used in a production environment

How to pass an array to awk inside the Linux shell script for processing

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.