Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding text files with less than 2000 rows and deleting them

Tags:

bash

shell

awk

I have A LOT of text files, with just one column.

Some text file have 2000 lines (consisting of numbers), and some others have less than 2000 lines (also consisting only of numbers).

I want to delete all the textiles with less than 2000 lines in them.

EXTRA INFO

The files that have less than 2000 lines, are not empty they all have line breaks till row 2000. Plus my files have some complicated names like: Nameofpop_chr1_window1.txt

I tried using awk to first count the lines of my text file, but because there are line breaks for every file I get the same result, 2000 for every file.

awk 'END { print NR }' Nameofpop_chr1_window1.txt

Thanks in advance.

like image 434
JM88 Avatar asked Nov 29 '25 20:11

JM88


1 Answers

You can use this awk to count non-empty lines:

awk 'NF{i++} END { print i }' Nameofpop_chr1_window1.txt

OR this awk to count only those lines that have only numbers

awk '/^[[:digit:]]+$/ {i++} END { print i }' Nameofpop_chr1_window1.txt

To delete all files with less than 2000 lines with numbers use this awk:

for f in f*; do
    [[ -n $(awk '/^[[:digit:]]+$/{i++} END {if (i<2000) print FILENAME}' "$f") ]] && rm "$f"
done
like image 177
anubhava Avatar answered Dec 02 '25 14:12

anubhava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!