"Linux" awk related

Source: Internet
Author: User

Add text content by field

A 3
B 4
C 5
A 8
D 2
C 6

Add the same data as the fields in the above content

awk -F ‘ ‘  ‘{sum[$1]+=$2}END{for(i in sum){print i, sum[i]}}‘  test.txt

Results:

A 11
B 4
C 11
D 2

Stitching two text by field

Text 1:

A 3
B 4
C 5

Text 2:

A high
C Middle
b Low

Stitching two text based on the first field

Results:

A 3 high
B 4 Low
C 5 Middle

Note:
NR: Represents the number of rows that have been read from all files.
FNR: Indicates the number of rows that have been read in the current file.
Give me a chestnut:
There are two files, the first file has 10 lines, the second file has 15 lines. When reading the 5th line of the first file, nr=5,fnr=5. When reading the 8th line of the second file, nr=18,fnr=8.
So NR==FNR means reading the first file.

"Linux" awk related

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.