Shell 脚本:如果文件中存在字符串

发布于 2024-12-14 00:28:44 字数 151 浏览 0 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最佳男配角 2024-12-21 00:28:44

您的问题有点不完整:

  • 您想查找字符串还是单词?那么当Othello出现时,算作hello吗?
  • 在您的问题中,when 后面有空格。这是故意的吗?
  • 您想知道所有三个单词是否都在文件中,或者其中一个单词就足够了吗?

一般的解决方案是使用 grepegrep 搜索文件中的文本。确切的命令行取决于上述问题的答案。

  • 要搜索单词(Othello 不算作 hello),您需要将 -w 选项传递给 grep。
  • 我假设空白是一个错误。

当您需要所有单词时,您可以执行egrep -wo 'hello|who|when' |排序-u。 egrep 命令查找给定单词的所有实例,并每行打印一个。那时,您将有许多重复项。因此,sort -u 命令对它们进行排序,并且仅保留唯一 行(这就是-u 的含义)。在一个完整的程序中,我会这样做:

filename="story.txt"
words=$(egrep -wo 'hello|who|when' "$filename" | sort -u)
n=$(echo "$words" | wc -l)
if [ $n = 3 ]; then
  echo "found all words in the file"
else
  echo "didn't find all words, only \""$words"\"."
fi

关于这一小段代码,我还有很多东西可以告诉你,以及为什么我完全那样写它,但对于初学者来说,这已经足够理解了。

但万一您需要一个简单的解决方案,并且文件很小,因此性能并不重要,您可以这样做:

filename="story.txt"
if egrep -wl 'hello' "$filename" 1>/dev/null; then
  if egrep -wl 'when' "$filename" 1>/dev/null; then
    if egrep -wl 'who' "$filename" 1>/dev/null; then
      echo "found all three words"
    fi
  fi
fi

[更新:]

第二个代码片段还检查给定文件是否包含全部 三个字。每个 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:

  • do you want to find strings or words? So when the word Othello appears, does that count as hello?
  • in your question there is whitespace behind the when. Is that intentional?
  • do you want to know whether all three words are in the file, or is one of the words enough?

The general solution is to use grep or egrep to search for text in a file. The exact command line depends on the answers to the above questions.

  • to search for words (Othello doesn't count as hello) you need to pass the -w option to grep.
  • I'm assuming thhat the whitespace was a mistake.

When you need all the words, you can do egrep -wo 'hello|who|when' | sort -u. The egrep command finds all instances of the given words, and prints them out one per line. At that point, you will have many duplicates. Therefore the sort -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:

filename="story.txt"
words=$(egrep -wo 'hello|who|when' "$filename" | sort -u)
n=$(echo "$words" | wc -l)
if [ $n = 3 ]; then
  echo "found all words in the file"
else
  echo "didn't find all words, only \""$words"\"."
fi

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:

filename="story.txt"
if egrep -wl 'hello' "$filename" 1>/dev/null; then
  if egrep -wl 'when' "$filename" 1>/dev/null; then
    if egrep -wl 'who' "$filename" 1>/dev/null; then
      echo "found all three words"
    fi
  fi
fi

[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) to egrep 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, the then branch is taken. The nice thing about the egrep 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文