如何使用 grep 在用户以字符串形式输入的文件中查找准确的单词?
例如,我需要选择要查找的词和要在其中查找它的文件。有人告诉我我真的很接近,但有些东西没有正常工作。我在 Linux 下使用 bash
shell。
这是我到目前为止所做的:
#!/bin/bash
echo "Find the file you want to search the word in?"
read filename
echo "Enter the word you want to find."
read word1
grep $word1 $filename
最佳答案
How can I use grep to find an exact word inside of a file entered by the user as string.
尝试使用 -F
选项。在 shell 上键入 man grep
以获得更多详细信息。
grep -F "$word1" "$filename"
建议用引号将搜索字符串和文件名括起来,以避免因空格而导致意外输出。
不确定为什么在最后一行有 fi
。不需要。
关于linux - 如何在 shell 脚本中使用 grep 在文件中查找单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19819221/