How awk passes a value to the shell

Source: Internet
Author: User

Today, writing scripts, encountering awk script to the shell of the case, the internet Google, found that there are some problems, through the pipeline, through Eval, feeling is very complex. So I thought to try it with read.

First, a test file Test.txt is constructed, and the contents are 1 2 3.

First try

' {print $ $} ' test.txt | Read a b C

But echo $a, found to be empty, failed.

Try it.

" 1 2 3 " | Read a b C

You can't assign a b c to a value.

It seems that read is not the use of, angry Google, in StackOverflow found a similar problem (StackOverflow true artifact Ah! )

Links: http://stackoverflow.com/questions/374687/why-is-echo-foo-read-a-echo-a-not-working-as-expected

There was a reply from the Lord mentioning that

Read a < < (echo foo)

You can assign a value to a, try it, sure enough.

And then try

Read a B c< < (echo foo goo hoo)

You can also assign a B c to a value separately! It's so exciting!

Then try

' {print $ $} ' test.txt)

Failed, the value of a becomes 123,b,c empty.

Why is this? Because awk does not print a space, it only prints 123, which of course is assigned to a.

So, add separators

' {print $ "" $ "" $ $} " ' test.txt)

It's a success!

Considering my awk is going to be a little more complicated.

1 ' {test=$1;test2=$2;test3=$3}end{print Test,test2,test3} ')

And it worked!

Thus, awk can pass values to the shell in such a way.

How awk passes a value to the 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.