awk打開多個文件的方法


1、當awk讀取的文件只有兩個的時候,比較常用的有三種方法
(1)awk 'NR==FNR{...}NR>FNR{...}' file1 file2

(2)awk 'NR==FNR{...}NR!=FNR{...}' file1 file2
(3)awk 'NR==FNR{...;next}{...}' file1 file2

next表示下一個命令不被執行

2、當awk處理的文件超過兩個時,顯然上面那種方法就不適用了。因為讀第3個文件或以上時,也滿足NR>FNR (NR!=FNR),顯然無法區分開來。
所以就要用到更通用的方法了:
(1)ARGIND 當前被處理參數標志: awk 'ARGIND==1{...}ARGIND==2{...}ARGIND==3{...}... ' file1 file2 file3 ...
(2)ARGV 命令行參數數組: awk 'FILENAME==ARGV[1]{...}FILENAME==ARGV[2]{...}FILENAME==ARGV[3]{...}...' file1 file2 file3 ...
(3)把文件名直接加入判斷: awk 'FILENAME=="file1"{...}FILENAME=="file2"{...}FILENAME=="file3"{...}...' file1 file2 file3 ...

舉例說明,這個例子在面試的時候問過。

a.txt中有一列:

A
B
C
D
E
b.txt中有兩列:

A hello world
A good morning
B what
C sad
D interview someone
D feeling bad
E not so good
E want to be happy
F fail to pass the exam
G good result
則打印出b.txt中的行,滿足b.txt中第一列等於a.txt的第一列。

#! /usr/bin/ksh

print "Method1:########"
awk 'ARGIND==1{test[NR]=$1}
    ARGIND==2{for(i in test){if(test[i]==$1){print $0}}}' a.txt b.txt

print "\nMethod2:########"
#the '{" must at the same line of the NR!=FNR
gawk 'NR==FNR{test[NR]=$1}
    NR!=FNR{ 
        for( i in test)
        {
            if(test[i]==$1)
                print $0
        }
           }' a.txt b.txt

print "\nMethod3:########"
gawk 'NR==FNR{test[NR]=$1}
        NR>FNR{ 
                for( i in test)
                {
                        if(test[i]==$1)
                                print $0
                }
        }' a.txt b.txt

print "\nMethod4:########"
gawk 'NR==FNR{test[NR]=$1;next}
        {
                for( i in test)
                {
                        if(test[i]==$1)
                                print $0
                }
        }' a.txt b.txt

print "\nMethod5:########"
gawk 'FILENAME==ARGV[1]{test[NR]=$1}
        FILENAME==ARGV[2]{
                for( i in test)
                {
                        if(test[i]==$1)
                                print $0
                }
        }' a.txt b.txt

print "\nMethod6:########"
gawk 'FILENAME=="a.txt"{test[NR]=$1}
        FILENAME=="b.txt"{
                for( i in test)
                {
                        if(test[i]==$1)
                                print $0
                }
        }' a.txt b.txt

輸出結果為:

Method1:########
A hello world
A good morning
B what
C sad 
D interview someone
D feeling bad
E not so good
E want to be happy

Method2:########
A hello world
A good morning
B what
C sad 
D interview someone
D feeling bad
E not so good
E want to be happy

Method3:########
A hello world
A good morning
B what
C sad 
D interview someone
D feeling bad
E not so good
E want to be happy

Method4:########
A hello world
A good morning
B what
C sad 
D interview someone
D feeling bad
E not so good
E want to be happy

Method5:########
A hello world
A good morning
B what
C sad 
D interview someone
D feeling bad
E not so good
E want to be happy

Method6:########
A hello world
A good morning
B what
C sad 
D interview someone
D feeling bad
E not so good
E want to be happy

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM