打印包含 bash 中的单词的行
Printing lines containing a word in bash
我正在写一个 script
并且我在输入中有 3 个参数 - folder name, type file, word
.
我想在任何 type file
中的 folder name
中搜索并输出那些包含单词的文件中的行。
例如,如果在 folder_name
中我有这些文件:image.png con.txt file.jpg
并且输入是 ./MyScript.sh folder_name txt hello
并且 con.txt
包含:
hey hello world
hola maybe ha
othello
output
将是:
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=""
ext=""
word=""
find "$folder" -type f -name "*.$ext" -exec grep -wFnH "$word" {} +
# or to get sorted output:
find "$folder" -type f -name "*.$ext" -exec grep -wFnH "$word" {} + |
sort -t: -k1
我正在写一个 script
并且我在输入中有 3 个参数 - folder name, type file, word
.
我想在任何 type file
中的 folder name
中搜索并输出那些包含单词的文件中的行。
例如,如果在 folder_name
中我有这些文件:image.png con.txt file.jpg
并且输入是 ./MyScript.sh folder_name txt hello
并且 con.txt
包含:
hey hello world
hola maybe ha
othello
output
将是:
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=""
ext=""
word=""
find "$folder" -type f -name "*.$ext" -exec grep -wFnH "$word" {} +
# or to get sorted output:
find "$folder" -type f -name "*.$ext" -exec grep -wFnH "$word" {} + |
sort -t: -k1