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

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

问题描述

我有一个包含 4 列数字的文件 (A.txt) 和另一个包含 3 列数字 (B.txt) 的文件.我需要解决以下问题:

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

  1. 查找 A.txt 中第 3 列的数字出现在 B.txt 的第 3 列中的任何位置的所有行.

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

假设我在一个目录中有很多文件,例如 A.txt,我需要为该目录​​中的每个文件运行它.

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

我该怎么做?

推荐答案

这是一个例子.创建以下文件并运行

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

输出应该是:

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天全站免登陆