Call Ksh variable in awk regular expression _ regular expression

Source: Internet
Author: User

If the variable pcname is defined in the Ksh, the environment variable is used in the AWK program embedded in Ksh, and it is used in regular expressions.

The easiest way to do this is to:

#!/bin/ksh pcname= "ap201" Nawk '/' "$pcname" '/{print $} ' $FILE

For more, see the article "AWK uses Shell variables " reproduced below:

I've been doing nothing lately, trying to write a shell script that uses AWK. You want to read/set the variables defined in the shell script in awk, search for resources, and summarize some of the stuff.

Reading variables

Read the shell variable in the following way
1> awk-v varname=varvalue ' ... '
2> awk ' ... {print ' ' ${user} ' ' "} ' ...
This way you can access system environment variables such as user, and custom shell variables. If there are no spaces or other special characters in these variables, you can write less than two double quotes: awk ' ... {print ' ${user} '} ' ...
3> awk ' ... {print environ[' USER ']} ... ' ...
But this method only accesses system variables

Setting variables

The method for setting the shell variable has not been found in awk, because, as in the method read above, the environment variables are a copy, no matter how many records are processed, regardless of how the main shell changes, the values of these variables are unchanged. Furthermore, it can be seen from the following experiment that executing the system function call command in awk is a separate shell thread, so you cannot rely on system to set external environment variables.
If your awk prints only one result, you can set variables in the shell environment in the following ways
Myvar= "' echo | awk ' {print ' AA/NBB/NCC '} ' '
Note that in this way, when awk prints multiple lines, a carriage return is replaced with a space and then assigned.


The experimental process is as follows:

$ VI tmp.sh
The edited content is as follows
#!/bin/bash Echo Echo "SHELL version: ${shell} ${bash_version}" echo AWK version: ' Awk-w version | Sed-n ' 1p ' "Echo myvar=" AAA BBB CCC DDD "echo" in shell:pid=$ "echo" in SHELL:/${myvar}=/"${myvar}/" "Echo Echo | Awk-v var1= "${myvar}" ' {print ' in Awk:how to print single quote: "'" ' "'" " NR print ' in AWK:-v:var1=/' var1 '/' print ' in awk:get:environ[/' user/']=/' ' environ[' USER '] '/' print ' in awk : get:environ[/"myvar/"]=/"environ[" MyVar "]"/"" Print "in Awk:get: ${user} =/" "" "${user}" ""/"print" in AWK: Get: ${myvar}=/"" "" ${myvar} "" "/" "Print system (" echo/"in Awk:system (): pid=$/" ")} ' myvar= ' echo | awk ' {print ' AA/NBB/NCC '} ' ' #/n is replaced by space character Echo ${myvar}

$ . tmp.sh
The output is as follows
SHELL VERSION:/bin/bash 3.2.25 (1)-release awk version:gnu awk 3.1.5 in shell:pid=10627 in SHELL: ${myvar}= "AAA BBB CCC DDD "in awk:how to print single quote: ' No.1 in AWK:-v:var1=" triple-BBB CCC DDD "in awk:get:environ[" USER "]= "Zhangll" in awk:get:environ["MyVar"]= "' in Awk:get: ${user} = ' Zhangll ' in Awk:get: ${myvar}=" AAA BBB CCC DDD "I N Awk:system (): pid=17468 AA BB cc 

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.