Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在bash中打印包含单词的行_Bash_Shell_Sh - Fatal编程技术网

在bash中打印包含单词的行

在bash中打印包含单词的行,bash,shell,sh,Bash,Shell,Sh,我正在写一个脚本,输入中有3个参数-文件夹名称、类型文件、word 我想搜索任何类型文件中的文件夹名,并输出这些文件中包含单词的行。 例如,如果在文件夹\u name中我有以下文件:image.png con.txt file.jpg,输入为/MyScript.sh folder\u name txt hello和con.txt包含: hey hello world hola maybe ha othello 输出将是: hey hello world 我开始写这篇文章: find $fol

我正在写一个
脚本
,输入中有3个参数-
文件夹名称、类型文件、word

我想搜索任何
类型文件中的
文件夹名
,并输出这些文件中包含单词的行。
例如,如果在
文件夹\u name
中我有以下文件:
image.png con.txt file.jpg
,输入为
/MyScript.sh folder\u name txt hello
con.txt
包含:

hey hello world
hola maybe ha
othello
输出将是:

hey hello world
我开始写这篇文章:

find $folder_name | sort d| #Check if the word is in the line | while read file_name; do
    cat "$file_name.$type_file" | # Print the line with the word  

但是我不知道如何将单词的每一行都签入并打印出来。

您可以像这样运行
find+grep
命令:

folder=“$1”
ext=“$2”
word=“$3”
查找“$folder”-键入f-name“*。$ext”-exec grep-wFnH“$word”{}+
#或获取排序输出:
查找“$folder”-键入f-name“*。$ext”-exec grep-wFnH“$word”{}+|
排序-t:-k1

不幸的是,这对我来说不起作用,你确定这段代码吗?100%经过测试并有效。现在请澄清
不起作用
,并提供您收到的错误的详细信息谢谢,它起作用了,但是如果我想按字典顺序浏览文件夹中的文件,我需要在代码中添加
排序
?我需要向代码中添加什么才能使其工作?我需要编写
find“$folder”-type f-name”*。$ext“| sort |-exec grep-h”$word“{}+| sort-t:-k1
,就像这样?@anubhava:由于OP需要查找字面上提供的单词,我认为它必须是
grep-nwFH”$word{
。您的解决方案打印行,其中包含与存储在
word
中的正则表达式匹配的子字符串。请参阅
grep
的手册页,特别是选项
-w
-F
。您的脚本使用名为
file\u name
的变量。它从哪里获得值?这也是一个输入参数,如
类型\文件
文件夹\名称
?如果是这样,请在问题中描述这一点,就像您对其他输入参数所做的一样。