Introduction to Linux awk and shell variable delivery __linux
Source: Internet
Author: User
First, awk uses ordinary variables in the shell
Method 1: Use the quotation mark method
Aa= "Hello"
awk ' BEGIN {print ' $aa '} '
Output $AA
awk ' BEGIN {print ' $aa '} '
Output Hello
awk ' BEGIN {print ' ' $aa '} '
Output empty
Aa= "hello! Coder "
awk ' BEGIN {print ' $aa '} '
Error, double quotes do not match, because space appears in the middle
awk ' BEGIN {print ' ' $aa ' '} '
Hello! can be displayed normally Coder
Method 2: Using the-V method
Awk-v varaa= "$aa" ' BEGIN {print Varaa} '
Awk-v varaa= $aa ' BEGIN {print Varaa} '
Can be normal display,-V or more convenient, more clear
Method 3: Through environment variables
Aa= "This is a test"; Export AA
awk ' begin{print environ[' AA ']} '
Normal
Note that setting the environment variable is export AA rather than export $AA
Method 4: Use awk ' script ' awkvar= ' shellvar ' filename
Aa= "This is a test"; Export AA
awk ' end{print BB ' bb= "$aa" a.txt
When the end is changed to begin, it cannot be used.
An array variable using the shell in awk
Aa= (a b c D e)
Awk-v bb= "$aa" ' Begin{print BB} '
Can only output a, estimated to be the first member of the array AA
Aa= (a b c D e)
Awk-v bb= "$aa" ' begin{for (i in BB) print Bb[i]} '
Error
Hint BB is not an array
Aa= (a b c D e)
Awk-v bb= "${aa[*]}" ' Begin{split (Bb,a, ""); for (i in A) print I "---" A[i]} '
Using the array in awk in the shell
Note: The array in the shell is an associative array, the subscript can only be a number, and the array in awk is an associative array, and the subscript can be a string
The variable name that awk defines in begin cannot be called index
Eval $ (awk ' BEGIN {xiabiao=0} {aa[$1]=$2} end{for (i in AA) {print ' aa[' Xiabiao ']= ' aa[i];xiabiao+=1}} ' A.txt)
echo ${#aa [@]}
Echo ${aa[@]}
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.