删除目录中的文件,其中匹配模式的数量低于 linux 上的阈值 [英] Remove file in dir where number of matching pattern below a threshold on linux

查看:21
本文介绍了删除目录中的文件,其中匹配模式的数量低于 linux 上的阈值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何删除目录中的所有 *.txt 文件,其中 grep '>'|wc -l 小于 2?

How can I remove all *.txt files within a directory, where grep '>' | wc -l is less than 2?

示例:

file1.txt
>BB
>AA
>HHD
file2.txt
>HS
file3.txt
>HHD
>GGS
>HHD

这里删除了file2.txt

Here file2.txt is removed

推荐答案

这个 awk + ​​xargs rm 解决方案可能适合你:

This awk + xargs rm solution may work for you:

awk 'FNR == 1 {if (fn != "" && n < 2) print fn; fn = FILENAME; n = 0} /^>/ {++n} n==2 {nextfile} END {if (n < 2) print fn}' *.txt | xargs rm

为了使其更具可读性:

awk 'FNR == 1 {
   if (fn != "" && n < 2)
      print fn
   fn = FILENAME
   n = 0
}
/^>/ {
   ++n
}
n == 2 {
   nextfile
}
END {
   if (n < 2)
      print fn
}' *.txt | xargs rm

这篇关于删除目录中的文件,其中匹配模式的数量低于 linux 上的阈值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆