Bash 刷新多个文件

Bash 刷新多个文件,bash,scripting,Bash,Scripting,我想grep一个目录中的多个文件,并将每个grep的输出收集到一个单独的文件中。因此,如果我grep 20个文件,我应该得到20个包含搜索项的输出文件。有人能帮我吗?谢谢。使用作为声明: for a in *.txt; do grep target $a >$a.out; done 只要一个呆呆的命令 gawk '/target/ {print $0 > FILENAME".out"}' *.txt 您可以只使用shell,不需要外部命令 for file in *.txt do

我想grep一个目录中的多个文件,并将每个grep的输出收集到一个单独的文件中。因此,如果我grep 20个文件,我应该得到20个包含搜索项的输出文件。有人能帮我吗?谢谢。

使用
作为声明:

for a in *.txt; do grep target $a >$a.out; done
只要一个呆呆的命令

gawk '/target/ {print $0 > FILENAME".out"}' *.txt

您可以只使用shell,不需要外部命令

for file in *.txt
do
    while read -r line
    do
        case "$line" in 
            *pattern*) echo $line >> "${file%.txt}.out";;
        esac
    done  < "$file"
done
用于*.txt中的文件
做
而read-r行
做
大写“$line”
*模式*)echo$line>>“${file%.txt}.out”;;
以撒
完成<“$file”
完成