2016-04-26 66 views
0

我有这样的结构::排序-n一个bash空间的内容分割的项目阵列

. 
├── b withe spaces.py 
├── c.py 
├── lib.py 
├── show.sh 
└── sub 
    └── a.py 

我想这个结果::

$ bash ./show.sh 
    9 lines c.py    => info from outside script 
    17 lines a.py    => info from outside script 
    300 lines b withe spaces.py => info from outside script 
1589 lines lib.py   => info from outside script 
1915 lines total    => info from outside script 

这很容易通过wc -l排序::

$ wc -l *.py */*.py | sort -n 
    9 c.py 
    17 sub/a.py 
    300 b withe spaces.py 
1589 lib.py 
1915 total 

所以我的出发点是::

array=(
$(find ./ -name "*.py" -print0 | while read -d $'\0' file 
    do 
    llines=$(cat $file | wc -l) 
    analyse='ouput analyse from some script' 
    printf "%s =\t%25s => %s" $llines $file $analyse 
    done 
)) 

for item in ${array[@]}; do echo $item; done| sort -n 
+2

发生了什么?这不行吗?没有让你预期的产出?实际的问题不存在。 –

回答

1

你不需要的阵列全天:

find ./ -name "*.py" -print0 | while read -d $'\0' file ; do 
    llines=$(wc -l < "$file") 
    analyse='ouput analyse from some script' 
    # ----- you missed quoting these ---->vvv 
    printf "%s =\t%s => %s\n" "$llines" "$file" "$analyse" 
done | sort -k1nr 
0

你可以这样做更容易:

find . -name "*sh" -print0 | wc --files0-from=- -l | sort -n | sed 's/\(^[0-9]\+\) \(.*\)/\1 lines \2 => info from outside script/' 

在这种情况下,你不会为每个文件执行wc分开,这将节省时间有点。 另外wc会计算你的总数。