Linux20180503 six-week fifth session (May 3) awk extension

Source: Internet
Author: User

Six week fifth session (May 3)
Using external shell variables in awk

In contrast, awk is more like a simple programming language

A=44echo "ABCD" | Awk-v get_a= $A ' {print get_a} '
Description: The-v option is used to define the parameter, which means that the value of variable A is given to get_a.
How many variables need to be assigned, and how many-V options are required. Equivalent to: Applied to the script:

awk merges a file
I have the need to merge two files with the same row in the first column. For example, there are two files with the following contents:
Cat 1.txt
1 AA
2 BB
3 ee
4 SS

Cat 2.txt
1 AB
2 CD
3 AD
4 BD
5 de

The result of the merge is:

3 ad EE
5 de

the commands implemented are:
awk ' Nr==fnr{a[$1]=$2}nr>fnr{print $0,a[$1]} ' 1.txt 2.txt

This involves

Explanation: NR represents the number of rows read, FNR indicates the current number of rows read
so in fact, nr==fnr means reading 2.txt. The same nr>fnr means when reading 1.txt
array A is actually the equivalent of a mapConcatenate a file multiple lines into a line

The use of Gsub functions in awk

awk intercepts specifying multiple fields as one row

Filter two or more keywords

Generate the following structure file with awk

awk Prints single quotation marks with print

Merge two files

Awk's Reference tutorial

Linux20180503 six-week fifth session (May 3) awk extension

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: 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.