Awk array usage

Source: Internet
Author: User

Two files. Find the line equal to file a from the first column of file B and Output
[[Email protected] ~] # Cat
1
3
6
7
[[Email protected] ~] # Cat B
1 2 3
2 3 4
3 4 5
4 5 6
5 6 7
6 7 8
7 8 9

Shell script
[[Email protected] ~] # Cat jieguo
#! /Bin/bash
Y = 'sed-n' P' A | WC-l' # number of a rows
X = 'sed-n' P' B | WC-l' # Number of B rows
For (I = 1; I <= $ Y; I ++ ))
Do
Value_a = 'sed-N $ {I} p a' # output by row, assigned to
For (j = 1; j <= $ X; j ++ ))
Do
Value_ B = 'awk' {print $1} 'B | sed-n "/$ J/P"' # first output column, assigned to B
If ["$ value_a" = "$ value_ B"] # judge whether value_a and value_ B are equal
Then
Value_c = 'sed-N $ {J} p B'
Echo $ value_c>/root/value_c # equal output of rows in B to value_c
Fi
Done
Done

Awk command line
Awk 'nr = FNR {k [$1] = $1} Nr> FNR {If (K [$1] = $1) {print $1, $2, $3} 'a B or
Awk 'nr = FNR {k [$1] = $1} Nr> FNR {If (K [$1] = $1) {print $0} 'a B
Nr: Total number of lines for awk to process files
FNR: number of lines that awk processes the current file
Array K [$1]: pay the value of file a to array K.

Condition Statement: Nr = FNR: execute the operation in {} on file.
NR> FNR: perform operations in {} on file B
K [$1] = $1 is the first domain in file.
If (K [$1] ==1 1) {print $1, $2, $3} is the 1, 2, and 3 fields in file B.
$0 is the current complete record.

Differences:

Script: nesting for is required to traverse the entire file. That is to say, to compare whether the first line of A and B is the same, each number in a needs to traverse the first line of data in B cyclically. X Y is required.

Awk: array K [$1] can be understood as converting the value of file a from a column to a line. In this way, when awk makes the if judgment, it is to find the target in a row of data, the for loop statement is not required to traverse the entire file like a column. It is to compare the data from the first line of B with the K array. If the data is the same, it is output. You only need to compare X times.

Running result:
[[Email protected] ~] #./Jieguo
1 2 3
3 4 5
6 7 8
7 8 9
[[Email protected] ~] # Awk 'nr = FNR {k [$1] = $1} Nr> FNR {If (K [$1] = $1) {print $0} 'a B
1 2 3
3 4 5
6 7 8
7 8 9

If the number of lines in a file is tens of thousands or hundreds of thousands, the script takes a lot of time. Awk second output result...

This article is from the "1058223494" blog, please be sure to keep this source http://4708705.blog.51cto.com/4698705/1543191

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.