如何保存的grep匹配的行? [英] How to save the lines of grep matches?
问题描述
我有这样一个grep的:
I have a grep like this:
iarr=`grep -Poh '.*INPUT' file.txt`
oarr=`grep -Poh '.*OUTPUT' file.txt`
因此,与此内容file.txt的的:
So with this content of file.txt:
INPUT hello.txt
OUTPUT stack.txt
INPUT stack.txt
OUTPUT hello.txt
INPUT overflow.txt
OUTPUT overflow.txt
OUTPUT byebye.txt
INPUT byebye.txt
INPUT nick.txt
OUTPUT jesus.txt
输出将是:
IARR
INPUT hello.txt
INPUT stack.txt
INPUT overflow.txt
INPUT byebye.txt
INPUT nick.txt
oarr
OUTPUT stack.txt
OUTPUT hello.txt
OUTPUT overflow.txt
OUTPUT byebye.txt
OUTPUT jesus.txt
很好,但我想知道我个文件名需要输入,会是怎样的输出,我的意思是:
Well, but I want to know what filenames I need in input and what will be the output, I mean:
- 如果一个输入文件在波纹管的实际线的线(同一个文件)的输出,那么该项目将在oarr删除。
- 如果一个输出文件具有高于实际线的线(同一个文件的)输入,则此项目将被从IARR删除。
使用这些条件,其结果必然是以下内容:
With these conditions, the result would be the following:
IARR
INPUT hello.txt
INPUT overflow.txt
INPUT nick.txt
oarr
OUTPUT stack.txt
OUTPUT byebye.txt
OUTPUT jesus.txt
我试图得到这样的结果,但我不知道如何存储在grep命令的正则表达式的especific匹配的行...这是我想解决这个问题,你有另一主意,这样做呢?
可逐行阅读它...
I'm trying to get this result, but I don't know how to store the line of an especific match of the regexp in the grep command... This is what i thought to solve this, do you have another idea to do it? May be reading it line by line...
推荐答案
如果我明白你的问题很好,你只需要在每个文件名的第一次出现。你可以做到这一点用awk:
If I understand your question well, you only need the first occurrence of each filename. You can achieve this using awk:
awk '!x[$2]++' file.txt
这样会给
INPUT hello.txt
OUTPUT stack.txt
INPUT overflow.txt
OUTPUT byebye.txt
INPUT nick.txt
OUTPUT jesus.txt
上,您可以继续处理。
on which you can continue your processing.
这篇关于如何保存的grep匹配的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!