Linux 在用户选择的文件中查找单词的 Shell 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19795643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:16:33  来源:igfitidea点击:

Shell script that finds a word within a file selected by a user

linuxbashshellgrep

提问by user2957449

I am training to do shell scripting as a hobby, I was stumbled on a task I was given by my tutor.

我正在接受作为业余爱好的 shell 脚本训练,我偶然发现了我的导师给我的任务。

The task is to make a shell script that you can enter a file name you want to search in and then it will respond if it exists or not; then if it exists you have another option to find a certain word in the file that exist in there the certain word must be shown.

任务是制作一个shell脚本,您可以输入您要搜索的文件名,然后它会响应是否存在;然后,如果它存在,您还有另一个选项可以在文件中找到某个单词,该单词必须显示在那里。

Here is what I've done so far. My tutor only gave me a hint it has something to do with grep??

这是我到目前为止所做的。我的导师只给了我一个暗示它与grep有关??

#!/bin/bash

echo "search the word you want to find"

read strfile

echo "Enter the file you wish to search in"
grep $strfile 

"strword" strfile

Here's the start of my improved work.

这是我改进工作的开始。

#!/bin/bash

printf "Enter a filename:
"
read str
 if [[ -f "$str" ]]; then

echo "The file '$str' exists."

else

echo "The file '$str' does not exists"

It seems the file isnt then asking for the word I want to find after searching for a file name.

在搜索文件名后,该文件似乎并没有询问我想找到的单词。

What am I doing wrong?

我究竟做错了什么?

!/bin/bash

!/bin/bash

read -p "enter a filename:" filename

read -p "输入文件名:" 文件名

if [[ -f $ filename ]] ;

如果 [[ -f $ 文件名 ]] ;

echo " the filename exists " then

回声“文件名存在”然后

read -p "enter the word you want to find. : word

read -p "输入要查找的单词。: word

[grep -c $word $filename

[grep -c $word $文件名

else echo "the file $str doesn't exist." fi

否则回显“文件 $str 不存在。” 菲

回答by damienfrancois

One solution:

一种解决方案:

#!/bin/bash

read -p "Enter a filename: " filename

if [[ -f $filename ]] ; then
    echo "The file $filename exists."
    read -p "Enter the word you want to find: " word
    grep "$word" "$filename"
else
    echo "The file $filename does not exist."
fi

Quite a few variants possible.

相当多的变体是可能的。

回答by Darth Hunterix

You can do the word counting part by:

您可以通过以下方式进行字数统计:

exits=$(grep -c $word $file)
if [[ $exists -gt 0 ]]; then
    echo "Word found"
fi

That's what you're missing, the rest of your script is ok.

这就是你所缺少的,你的脚本的其余部分没问题。

"grep -c" counts lines which contain $word, so a file:

"grep -c" 计算包含 $word 的行,因此是一个文件:

word word other word
word
nothing

will produce value "2". Putting grep in $() let's you store the result in a variable. I think the rest is self-explanatory, especially, that you already have it in you post :)

将产生值“2”。将 grep 放入 $() 可以让您将结果存储在变量中。我认为其余的都是不言自明的,尤其是你已经在你的帖子中:)

回答by Ranjithkumar T

Try,

尝试,

 # cat find.sh
 #!/bin/bash
 echo -e "Enter the file name:"
 read fi
 echo -e "Enter the full path:"
 read pa
 se=$(find "$pa" -type f -name "$fi")
 co=$(cat $se | wc -l)
 if [ $co -eq 0 ]
 then
 echo "File not found on current path"
 else
 echo "Total file found: $co"
 echo "File(s) List:"
 echo "$se"
 echo -e "Enter the word which you want to search:"
 read wa
 sea=$(grep -rHn "$wa" $se)
 if [ $? -ne 0 ]
 then
 echo "Word not found"
 else
 echo "File:Line:Word"
 echo "$sea"
 fi
 fi

output:

输出:

 # ./find.sh
 Enter the file name:
 best
 Enter the full path:
 .
 Total file(s) found: 1
 File(s) List:
 ./best
 Enter the word which you want to search:
 root
 File:Line:Word
 ./best:1:root
 # ./find.sh
 Enter the file name:
 besst
 Enter the full path:
 .
 File not found on current path