【问题标题】:Show file contents after searching word Done [closed]搜索完成后显示文件内容[关闭]
【发布时间】:2014-10-08 12:48:34
【问题描述】:

我需要在搜索一个单词后显示文件内容。如果找到该单词,则显示该文件。

我的代码如下:

GNU nano 2.2.6 文件:工作

#!/bin/bash


while read -p "Welcome what would you like to do (S) to search or (Q) to quit  "  option
do
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"
                cat $file
                else
                echo "$word NOT found in $file"
                fi
                ;;

        "Q")    echo "Goodbye!"
                exit ;;

          *)    echo "invalid option" ;;
esac
done

【问题讨论】:

  • 你的问题英语不好理解。
  • 对不起,我有阅读障碍
  • 好的。所以试着用另一种方式重新表述或描述你的问题。不要害羞多余。

标签: linux bash file text


【解决方案1】:

替换

echo $file

cat $file

【讨论】:

    【解决方案2】:

    我相信你正在寻找命令cat $file。将其粘贴在您的 if 块内。

    【讨论】:

      【解决方案3】:

      我需要加载文件的内容而不是加载文件。

      不访问文件就无法访问文件的内容。

      【讨论】:

        【解决方案4】:
        grep -l word file | xargs -r cat
        

        如果找到单词,则显示文件内容。这也显示了文件名

        grep -l word file | xargs -r -i bash -c "echo {}:; cat {}"
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-11-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-01-29
          • 1970-01-01
          相关资源
          最近更新 更多