Shell 脚本:如果文件中存在字符串
我是 shell scriptng 的新手,我想检查文件中是否存在 3 个字符串(“hello”、“who”、“when” 等)。
当我用 google 搜索 awk、cat、grep 等时,我发现了很多方法,什么是最好的方法以及我该怎么做。
我只需要知道字符串是否存在。
I am a newbie to shell scriptng and I want to check if 3 strings("hello","who","when " etc) are present in a file.
I find many ways when I google out awk,cat ,grep etc ,What can be the best way and how Can I do it.
I just need to know if the strings are present or not .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题有点不完整:
Othello
出现时,算作hello
吗?when
后面有空格。这是故意的吗?一般的解决方案是使用
grep
或egrep
搜索文件中的文本。确切的命令行取决于上述问题的答案。Othello
不算作hello
),您需要将-w
选项传递给 grep。当您需要所有单词时,您可以执行egrep -wo 'hello|who|when' |排序-u。
egrep
命令查找给定单词的所有实例,并每行打印一个。那时,您将有许多重复项。因此,sort -u
命令对它们进行排序,并且仅保留唯一 行(这就是-u
的含义)。在一个完整的程序中,我会这样做:关于这一小段代码,我还有很多东西可以告诉你,以及为什么我完全那样写它,但对于初学者来说,这已经足够理解了。
但万一您需要一个简单的解决方案,并且文件很小,因此性能并不重要,您可以这样做:
[更新:]
第二个代码片段还检查给定文件是否包含全部 三个字。每个
if
子句都会检查其中一个单词。egrep
的选项-l
(小写 ell)可能会更快,但您可能根本不需要该选项。通常
egrep
打印与给定表达式匹配的所有行(在本例中为三个单词)。由于我们不需要该输出,因此我们使用箭头运算符>
将其重定向到名为/dev/null
的特殊文件。您写入该文件的任何内容都将被丢弃。if
语句接受另一个命令作为其参数,如果该命令成功返回,则采用then
分支。egrep
命令的好处是,当文件中包含给定的搜索表达式时,它会成功返回,因此这两件事完美地结合在一起。要进一步阅读,您应该尝试 Open Group 网站上的参考文档:http://www. google.com/search?q=opengroup+grep
Your question is a little incomplete:
Othello
appears, does that count ashello
?when
. Is that intentional?The general solution is to use
grep
oregrep
to search for text in a file. The exact command line depends on the answers to the above questions.Othello
doesn't count ashello
) you need to pass the-w
option to grep.When you need all the words, you can do
egrep -wo 'hello|who|when' | sort -u
. Theegrep
command finds all instances of the given words, and prints them out one per line. At that point, you will have many duplicates. Therefore thesort -u
command sorts them and only keeps the unique lines (that's what the-u
means). In a complete program, I would do it as follows:There's a lot more that I could tell you about this little piece of code, and why I wrote it exactly like that, but for a beginner, it's already enough to understand.
But just in case that you need a simple solution and the file is small anyway, so performance is not critical, you can do this:
[Update:]
This second code snippet also checks whether the given file contains all three words. Each of the
if
clauses checks for one of the words. The option-l
(lowercase ell) toegrep
makes it potentially faster, but you probably don't need that option at all.Normally
egrep
prints all lines that match the given expressions (your three words in this case). Since we don't need that output, we redirect it using the arrow operator>
to a special file called/dev/null
. Whatever you write into that file is discarded.The
if
statement takes another command as its argument, and if that command returns successfully, thethen
branch is taken. The nice thing about theegrep
command is that it returns successfully iff the given search expression is contained in the file, so these two things perfectly fit together.For further reading you should try the reference documentation from the Open Group website: http://www.google.com/search?q=opengroup+grep