【问题标题】:Linux: using grep to find a word in a file then printing if it does or does not have that word in the fileLinux:使用 grep 在文件中查找单词,然后打印文件中是否包含该单词
【发布时间】:2023-03-21 18:45:01
【问题描述】:

我正在尝试使用 grep 命令,但是我不知道如何在“grep $word $file”之后继续,或者即使它会起作用。我需要做的是获取程序,以便在他们输入一个单词和一个文件后,如果该单词在文件中,则程序会回显“您输入的单词在您输入的文件中”。而且我还需要它来打印“对不起,您输入的单词不在您输入的文件中”,如果您能提供帮助,那将非常有帮助,谢谢

#!/bin/bash
echo "Welcome what please type in what you would like to do!"
echo "You can:"
echo "Search for words in file type (S)"
echo "Quit (Q)"
read option
while $option in
    do
            S)      echo "What is the name of the file you would like to search for?"
                    read file
                    echo "What word would you like to find in the file?"
                    read word
                    grep $word $file


            Q)      echo "Goodbye!"
                    exit
    esac
done

【问题讨论】:

    标签: linux string bash grep find


    【解决方案1】:

    使用grep -q 选项禁用匹配单词的打印,使用echo $? 获取grep 的返回值。 0 是找到匹配的返回值,否则将返回 1

    #!/bin/bash
    echo "Welcome what please type in what you would like to do!"
    echo "You can:"
    echo "Search for words in file type (S)"
    echo "Quit (Q)"
    read option
    case $option in
                S)      echo "What is the name of the file you would like to search for?"
                        read file
                        echo "What word would you like to find in the file?"
                        read word
                        grep -q $word $file
                        if [ $? -eq 0 ]; then
                          echo "$word found in $file"
                        else
                          echo "$word NOT found in $file"
                        fi
                        ;;
    
    
                Q)      echo "Goodbye!"
                        exit
    esac
    

    如果你想匹配整个单词,你也可以使用grep -w。所以你的grep 看起来像

                    grep -wq $word $file
                    if [ $? -eq 0 ]; then
                      echo "$word found in $file"
                    else
                      echo "$word NOT found in $file"
    

    【讨论】:

    • 我在第 9 行遇到错误,您能帮我吗?
    • 这是错误 ./fin2: line 9: syntax error near unexpected token )' ./fin2: line 9: S) echo "What is the name of the file you want to search?"'
    • 你能指定错误和抛出错误的行吗?
    • ./fin2: line 9: syntax error near unexpected token )' ./fin2: line 9: S) echo "What is the name of the file you want to search?"'
    • ./fin2:第 8 行:意外标记附近的语法错误 newline' ./fin2: line 8: do'
    【解决方案2】:

    使用&&|| 的简单一个衬里

    grep -q $word $file && echo "found the word" || echo "not found the word"
    

    例如

    $ echo hello | grep -q hell && echo "found the word" || echo "not found the word"
    found the word
    
    $ echo hello | grep -q help && echo "found the word" || echo "not found the word"
    not found the word
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-13
      • 1970-01-01
      • 2020-06-25
      • 2011-05-06
      • 1970-01-01
      • 2011-10-28
      • 2012-03-02
      • 1970-01-01
      相关资源
      最近更新 更多