grep 用于文件中不同行的多个字符串(即整个文件,而不是基于行的搜索)? [英] grep for multiple strings in file on different lines (ie. whole file, not line based search)?

查看:10
本文介绍了grep 用于文件中不同行的多个字符串(即整个文件,而不是基于行的搜索)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在任何行中搜索包含 DanskSvenskaNorsk 字样的文件,并带有一个可用的返回码(因为我真的只喜欢包含字符串的信息,我的单行比这个更进一步).

I want to grep for files containing the words Dansk, Svenska or Norsk on any line, with a usable returncode (as I really only like to have the info that the strings are contained, my one-liner goes a little further then this).

我有很多这样的行的文件:

I have many files with lines in them like this:

Disc Title: unknown
Title: 01, Length: 01:33:37.000 Chapters: 33, Cells: 31, Audio streams: 04, Subpictures: 20
        Subtitle: 01, Language: ar - Arabic, Content: Undefined, Stream id: 0x20, 
        Subtitle: 02, Language: bg - Bulgarian, Content: Undefined, Stream id: 0x21, 
        Subtitle: 03, Language: cs - Czech, Content: Undefined, Stream id: 0x22, 
        Subtitle: 04, Language: da - Dansk, Content: Undefined, Stream id: 0x23, 
        Subtitle: 05, Language: de - Deutsch, Content: Undefined, Stream id: 0x24, 
(...)

这是我想要的伪代码:

for all files in directory;
 if file contains "Dansk" AND "Norsk" AND "Svenska" then
 then echo the filename
end

这样做的最佳方法是什么?可以在一行上完成吗?

What is the best way to do this? Can it be done on one line?

推荐答案

您可以使用:

grep -l Dansk * | xargs grep -l Norsk | xargs grep -l Svenska

如果您还想在隐藏文件中查找:

If you want also to find in hidden files:

grep -l Dansk .* | xargs grep -l Norsk | xargs grep -l Svenska

这篇关于grep 用于文件中不同行的多个字符串(即整个文件,而不是基于行的搜索)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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