一起使用grep和awk [英] Using grep and awk together

查看:730
本文介绍了一起使用grep和awk的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件(A.txt),数字上有4列,另一个文件上有3列数字(B.txt)。我需要解决以下问题:


  1. 查找A.txt中的所有行,其第3列的数字出现在任何位置在B.txt的第3列中。假设我在目录中有许多像A.txt一样的文件。我需要为该目录​​中的每个文件运行此操作。

  2. 我该怎么做?

    解决方案

    下面是一个例子。创建以下文件并运行

      awk -f c.awk B.txt A * .txt 

    c.awk

      FNR == NR {
    s [$ 3]
    next
    }

    $ 3 in s {
    print FILENAME,$ 0

    A1.txt $ b

      1 2 3 
    1 2 6
    1 2 5

    A2.txt

      1 2 3 
    1 2 6
    1 2 5



    B.txt strong>

      1 2 3 
    1 2 5
    2 1 8

    输出结果应该是:

      A1.txt 1 2 3 
    A1.txt 1 2 5
    A2.txt 1 2 3
    A2.txt 1 2 5


    I have a file (A.txt) with 4 columns on numbers and another file with 3 columns of numbers (B.txt). I need to solve the following problems:

    1. Find all lines in A.txt whose 3rd column has a number that appears any where in the 3rd column of B.txt.

    2. Assume that I have many files like A.txt in a directory. I need to run this for every file in that directory.

    How do I do this?

    解决方案

    Here is an example. Create the following files and run

    awk -f c.awk B.txt A*.txt 
    

    c.awk

    FNR==NR {
        s[$3]
        next
    }
    
    $3 in s {
        print FILENAME, $0
    }
    

    A1.txt

    1 2 3
    1 2 6
    1 2 5
    

    A2.txt

    1 2 3
    1 2 6
    1 2 5
    

    B.txt

    1 2 3
    1 2 5
    2 1 8
    

    The output should be:

    A1.txt 1 2 3
    A1.txt 1 2 5
    A2.txt 1 2 3
    A2.txt 1 2 5
    

    这篇关于一起使用grep和awk的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆