按字数分两列打印前N个文件



我想制作一个脚本,从两个目录中打印顶部n文件的文件名(n是我在命令行中提供的文件数(,按它们的字数顺序排列。然而,我最大的问题是它们的展示方式。

假设我的命令行如下:

myscript.sh 5 dir1 dir2

输出应该有两列:左边是从dir1开始按降序排列的前5个文件,右边是从dir2开始按降序排序的前5名文件。

这就是我在代码方面所拥有的,但是我缺少了一些东西。我认为pr -m -t应该做我想做的事,但我无法让它发挥作用。

#!/bin/bash
dir=$1
dir2=$2
for files in "$dir"
do  
find ./reuters-topics/$dir -type f -exec wc -l {} + | sort -rn |head -n 15    
done
for files in "$dir2"
do
find ./reuters-topics/$dir2 -type f -exec wc -l {} + | sort -rn | head -n 15 

done

这是fish:中的一个解决方案

for i in (find . -type f); wc -l $i; end | sort -rn | head -n15 | awk '{print $2 "t" $1}'

正如您所看到的,重新排序(首先是文件名,其次是字数(是由awk完成的。作为分隔符,我使用一个制表符:

awk '{print $2 "t" $1}'

顺便说一句,我的循环和你的find调用之间的区别在于,我没有得到";合计";输出中的行。我没有测试这个(包括awk(是否也适用于名称中有空格的文件。

#!/usr/bin/env bash
_top_files_by_words_usage() {
local usage=""
read -r -d '' usage <<-"EOF"
Usage:
top_files_by_words <show_count> <dir1> <dir2>
EOF
1>&2 printf "%sn" "$usage"
}
top_files_by_words() {
if (( $# != 3 )) || [[ "$1" != +([0-9]) ]]; then
_top_files_by_words_usage
return 1
fi
local -i showCount=0
local dir1=""
local dir2=""
showCount="$1"
dir1="$2"
dir2="$3"
shopt -s extglob

if [[ ! -d "$dir1" ]]; then
1>&2 printf "directory '%s' does not exist or is not a directoryn" "$dir1"
return 1
fi
if [[ ! -d "$dir2" ]]; then
1>&2 printf "directory '%s' does not exist or is not a directoryn" "$dir2"
return 1
fi
local -a out1=()
local -a out2=()
IFS=$'n' read -r -d '' -a out1 < <(find "$dir1" -type f -exec wc -w {} ; | sort -k 1gr | head -n "$showCount")
IFS=$'n' read -r -d '' -a out2 < <(find "$dir2" -type f -exec wc -w {} ; | sort -k 1gr | head -n "$showCount")
local -i i=0
local -i maxLen=0
local -i len=0;
for ((i = 0; i < showCount; ++i)); do
len="${#out1[$i]}"
if (( len > maxLen )); then
maxLen=$len
fi
# len="${#out2[$i]}"
# if (( len > maxLen )); then
#   maxLen=$len
# fi
done
for (( i = 0; i < showCount; ++i)); do
printf "%-*.*s %sn" "$maxLen" "$maxLen" "${out1[$i]}" "${out2[$i]}"
done

return 0
}
top_files_by_words "$@"
$ ~/tmp/count_words.bash 15 tex tikz
2309328 tex/resume.log                                       9692402 tikz/tikz-Graphics in LaTeX with TikZ.mp4
2242997 tex/resume_cv.log                                    2208818 tikz/tikz-Tikz-Graphs and Automata.mp4
2242969 tex/cover_letters/resume_cv.log                       852631 tikz/tikz-Drawing Automata with TikZ in LaTeX.mp4
73859 tex/pgfplots/plotdata/heightmap.dat                   711004 tikz/tikz-tutorial.mp4
49152 tex/pgfplots/lena.dat                                 300038 tikz/.ipynb_checkpoints/TikZ 11 Design Principles-checkpoint.ipynb
43354 tex/nancy.mp4                                         300038 tikz/TikZ 11 Design Principles.ipynb
31226 tex/pgfplots/pgfplotstodo.tex                         215583 tikz/texample/bridges-of-konigsberg.svg
26000 tex/pgfplots/plotdata/ou.dat                          108040 tikz/Visual TikZ.pdf
20481 tex/pgfplots/pgfplotstable.tex                         82540 tikz/worldflags.pdf
19571 tex/pgfplots/pgfplots.reference.3dplots.tex            37608 tikz/texample/india-map.tex
19561 tex/pgfplots/plotdata/risingdrop3d_coord.dat           35798 tikz/.ipynb_checkpoints/TikZ-checkpoint.ipynb
19561 tex/pgfplots/plotdata/risingdrop3d_vel.dat             35656 tikz/texample/periodic_table.svg
18207 tex/pgfplots/ChangeLog                                 35501 tikz/TikZ.ipynb
17710 tex/pgfplots/pgfplots.reference.markers-meta.tex       25677 tikz/tikz-Graphics in LaTeX with TikZ.info.json
13800 tex/pgfplots/pgfplots.reference.axisdescription.tex    14760 tikz/tikz-Tikz-Graphs and Automata.info.json

column可以按列并排打印文件。您可以使用具有CCD_ 4的过程替换;文件";是实时命令,而不是实际文件。

#!/bin/bash
top-files() {
local n="$1"
local dir="$2"
find "$dir" -type f -exec wc -l {} + |
head -n -1 | sort -rn | head -n "$n"
}
n="$1"
dir1="$2"
dir2="$3"
column <(top-files "$n" reuters-topics/"$dir1") 
<(top-files "$n" reuters-topics/"$dir2")

最新更新