简体   繁体   English

基于非空行对文件进行排序 Linux

[英]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.我想根据非空行以相反的顺序递归地对找到的非空 .py 文件(来自当前目录)进行排序。 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.我更喜欢单行而不是 bash 脚本。 Thank you in advance先感谢您

Find py files, count non-empty lines with grep, revert columns with awk, and sort in inverse numeric order:查找 py 文件,用 grep 计算非空行,用 awk 还原列,并按倒数顺序排序:

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 | sort -n将帮助您完成大部分工作。

wc -l *.py | grep -vF ' 0 ' | sort -n wc -l *.py | grep -vF ' 0 ' | sort -n will eliminate the empty files. wc -l *.py | grep -vF ' 0 ' | sort -n将消除空文件。

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' '

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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