Awk 为目录中的多个文件设置阈值

Awk 为目录中的多个文件设置阈值,awk,sed,Awk,Sed,我有一个目录中的文件列表。例如,下面的文件是每个文件的名称,第一行显示在每个文件中,其他几行并不重要 第一组: 第2组: 第3组: 我想读取每个文件的第一行,检查第一个值是否等于8,第二个值是否大于500。如果满足此条件,请将文件名打印到新的文本文件中 结果 我试着用 for f in *.Group; do head -n1 *.Group > new-file; done 这将为我提供一个文件,其中包含头名称和目录中每个文件的第一行 => Group1 <= 8 32

我有一个目录中的文件列表。例如,下面的文件是每个文件的名称,第一行显示在每个文件中,其他几行并不重要

第一组:

第2组:

第3组:

我想读取每个文件的第一行,检查第一个值是否等于8,第二个值是否大于500。如果满足此条件,请将文件名打印到新的文本文件中

结果

我试着用

for f in *.Group; 
do head -n1 *.Group > new-file;
done 
这将为我提供一个文件,其中包含头名称和目录中每个文件的第一行

=> Group1 <=
8 325

=> Group2 <=
8 560

=> Group3 <=
7 650
现在,我想根据阈值筛选文件,但不确定如何将所有标题转换为第一列,将相应的值转换为第二列。然后很容易应用阈值并过滤文件。或者有更好的方法吗?

您可以使用awk:

说明:

# FNR contains the number of line of the current(!) input
# file. Check if the conditions are met and print the filename
FNR==1 && $1==8 && $2>500 {
    print FILENAME
}
上述解决方案应适用于任何版本的awk。如果您有GNU awk,则可以利用nextfile表达式。处理完第一行后,使用它可以跳过输入文件的其余行:

# Check if the conditions are met and print the filename in that case
$1==8 && $2>500 {
    print FILENAME
}

# Skip the remaining lines in the current file and continue
# with the next file
{
    nextfile
}

因此,我检查了文件awk-F'{print NF}'Group1第一行中的字段数,它显示为6,因此在本例中,解决方案可能有点不同,因为它们不仅仅是两个单独的字段。-F将该行拆分为单独的字符。Group1有6个字符,这是正确的。那你是什么意思?很高兴听到这个!那么您的文件名是Group1还是Group1.Group?Group1,group2,。。等等
for f in *.Group; 
do head -n1 *.Group > new-file;
done 
=> Group1 <=
8 325

=> Group2 <=
8 560

=> Group3 <=
7 650
awk 'FNR==1 && $1==8 && $2>500{print FILENAME}' *.Group > Result
# FNR contains the number of line of the current(!) input
# file. Check if the conditions are met and print the filename
FNR==1 && $1==8 && $2>500 {
    print FILENAME
}
# Check if the conditions are met and print the filename in that case
$1==8 && $2>500 {
    print FILENAME
}

# Skip the remaining lines in the current file and continue
# with the next file
{
    nextfile
}