The method of variable passing between awk and Shell

Source: Internet
Author: User

The method of variable passing between awk and Shell


awk is a good thing to do with Linux, and it's very handy to work with some text files. under Linux, it's often the case that you deal with the shell , so the variables between awk and The shell are sometimes necessary, so simply summarize.

awk use shell in the variables in


one , "' $var '"

In this way, you don't have to change the habit of using the awk program, which is commonly used by foreigners . such as :

var= "Test"
awk ' begin{print ' $var ' "} '

This notation is actually a constant that is double-parenthesis into parentheses , passed to awk.

if the var contains spaces , in order for the shell not to use the space as a sub-Geff , it should be used as follows :

Var= "This is a test"
awk ' Begin{print ' "$var" ' "} '
two . ' $var '

This is similar to the previous one . If the variable contains spaces , it becomes ' "" $var "" is more reliable .
Export variable , using environ["var" form , Get the value of an environment variable

For example :
Var= "This is a test"; Export var;

awk ' Begin{print environ["var"]} '
Four, You can use awk the- v Options (if the number of variables is not many, personal preference for this type of writing)

For example :
Var= "This is a test"
Awk-v awk_var= "$var" ' BEGIN {print Awk_var} '

This then passes the system variable var to the awk variable awk_var.

awk to the shell Variable Pass value

The idea of passing a variable from awk to the shell is nothing more thanusing awk (Sed/perl and so on ) to output several strips Shell Command, and then use the shell to execute the commands.

Eval $ (awk ' begin{print ' var1= ' str1′;var2= ' str2′ "} ')

or eval $ (awk ' {printf ("var1=%s; var2=%s; var3=%s; ", $1,$2,$3)} ' Abc.txt)

Variables such as Var1,var2 can then be used in the current shell .

echo "var1= $var 1-–var2= $var 2″

Resources:

Http://developer.51cto.com/art/200509/3781.htm

Http://www.ixpub.net/thread-1457438-1-1.html


This article is from the "Cool bit Linux" blog, so be sure to keep this source http://coolbyte.blog.51cto.com/8289854/1687609

The method of variable passing between awk and Shell

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.