一起使用grep和awk [英] Using grep and awk together
问题描述
我有一个文件(A.txt),数字上有4列,另一个文件上有3列数字(B.txt)。我需要解决以下问题:
-
查找A.txt中的所有行,其第3列的数字出现在任何位置在B.txt的第3列中。假设我在目录中有许多像A.txt一样的文件。我需要为该目录中的每个文件运行此操作。
我该怎么做? Find all lines in A.txt whose 3rd column has a number that appears any where in the 3rd column of B.txt.
Assume that I have many files like A.txt in a directory. I need to run this for every file in that directory.
下面是一个例子。创建以下文件并运行
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:
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屋!