zoukankan      html  css  js  c++  java
  • 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 "
    Method2:########"
    #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 "
    Method3:########"
    gawk 'NR==FNR{test[NR]=$1}
            NR>FNR{ 
                    for( i in test)
                    {
                            if(test[i]==$1)
                                    print $0
                    }
            }' a.txt b.txt
    
    print "
    Method4:########"
    gawk 'NR==FNR{test[NR]=$1;next}
            {
                    for( i in test)
                    {
                            if(test[i]==$1)
                                    print $0
                    }
            }' a.txt b.txt
    
    print "
    Method5:########"
    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 "
    Method6:########"
    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
  • 相关阅读:
    PHP打开错误提示和关闭错误提示的方法
    squid的简单介绍
    伪静态与重定向--RewriteRule
    PHP操作Redis常用技巧总结
    爱漂泊人生 30个php操作redis常用方法代码例子
    Mysql与Redis的同步实践
    Linux 命令之grep
    Linux 命令之sed
    Linux shell 计算两个文件的并集、交集、差集
    Linux 软链接的创建、删除和更新
  • 原文地址:https://www.cnblogs.com/Berryxiong/p/6209324.html
Copyright © 2011-2022 走看看