对整个文件使用grep [英] Using grep for an entire file

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

问题描述

我有一个包含250行的列表.我必须通过Web服务器运行所有这些文件以获得输出列表.但是,此列表返回的行比我感兴趣的多得多.说,我的list.txt是:

I've got a list with 250 lines in it. I have to run all of them through a web server to get a list of output. This list, however returns many more lines, than I'm interested in. Say, my list.txt is:

a.1
b.1
etc

然后输出为output.txt:

then the output is output.txt:

a.1 a b c
a.2 b a b
a.3 d k o
b.1 b o p
b.2 o i y
b.3 p i y
etc

是否可以使用grep命令在output.txt中的list.txt中搜索所有单词,然后生成通缉"列表wanted.txt?

Is it possible to use the grep command to search for all words in list.txt in the output.txt and then generate "the wanted" list wanted.txt?

我需要output.txt

我已经在AskUnuntu上问了这个问题,但是他们已经把我送到了这里.我被建议使用此命令

I've asked this question on AskUnuntu but they've sent me here. I was suggested this command

$ grep -wFf list.txt output.txt

,但是提示我输入grep: out of memory.

对于像我这样的初学者,有没有简单的方法可以做到这一点?

Is there a simplish way to do this for a beginner like me?

推荐答案

此单行代码读取list.txt中的每一行,并在output.txt中进行抓取(假设您只想将开头与单词匹配的行,并且仅当它本身是一个词时):

This one-liner reads every line in list.txt and greps for it in output.txt (assuming you want to match only lines with the word at the beginning, and only if it is a word of its own):

while read word ; do grep -w "^$word" output.txt ; done < list.txt > wanted.txt

使用提供的示例输入以及以下几行

Using the provided example input plus the following lines

a.1bar
fooa.1
foo a.1 bar

wanted.txt包含:

a.1 a b c
b.1 b o p

请注意,此解决方案不如使用grep -f优雅,但可以解决您的特定内存问题(未经测试).

Note that this solution is less elegant than using grep -f, but may address your specific memory problem (untested).

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

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