我需要在搜索单词后显示文件内容。如果找到该单词,则显示该文件。
我的代码如下:
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
答案 0 :(得分:1)
替换
echo $file
与
cat $file
答案 1 :(得分:0)
我相信你正在寻找命令cat $file
。将其粘贴在if
块内。
答案 2 :(得分:0)
我需要在加载文件时加载文件所说的内容。
如果不访问该文件,则无法访问该文件的内容。
答案 3 :(得分:0)
grep -l word file | xargs -r cat
如果找到单词,显示文件内容。这也显示了文件名
grep -l word file | xargs -r -i bash -c "echo {}:; cat {}"