简体   繁体   中英

Sorting files based on non empty lines Linux

I want to sort recursively found non-empty .py files (from current directory) in reverse order, based on the non-empty lines. If multiple files have equal number of non-blank lines, than the order should be alphabetical. All i have is:

find -P . -name '*.py' ! size 0 -print | xargs cat | sed '/^\\s*$/d' | wc -l

But this is not working and I don't know how to sort. I would prefer a one-liner instead of a bash script. Thank you in advance

Find py files, count non-empty lines with grep, revert columns with awk, and sort in inverse numeric order:

find -name '*.py' -exec grep -v '^$' -c {} -H \; | \
    awk -F: '{print $2, $1}' | \
    sort -nr

wc -l *.py | sort -n wc -l *.py | sort -n will get you most of the way there.

wc -l *.py | grep -vF ' 0 ' | sort -n wc -l *.py | grep -vF ' 0 ' | sort -n will eliminate the empty files.

Neither will handle recurse lookups, but that's not specified in your question, and I don't know how you'd go about alphabetizing in that case.

I think this does what you are asking:

 wc -l *.py | grep -vF ' 0 ' | sort -nr -k1,2 -t' '

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM