列出所有文本文件中的最高数字(目录中的多个文本文件)
List highest number inside all text files (multiple text files in directory)
我有一个包含多个文本文件的目录。
例如:
name1.txt
name2.txt
name3.txt
etc.. etc..
每个文本文件包含 1 行,该行是一个数字,即“10”
我想知道是否有可能以某种方式回显所有文本文件中排名前 10 位的文本文件名。
我想知道这是否可以通过 PHP 实时完成或通过 bash 脚本/cron 定期更新
谢谢!
这不是最有效的想法,但假设您不能使用数据库(否则您可能会):
<?php
$files = scandir('path/to/files/directory');
$rows = Array();
foreach($files as $file){
array_push($rows,file_get_contents('path/to/files/directory/'.$file);
}
arsort($rows);
$i = 0;
foreach($rows as $key => $row){
if($i <= 10){
echo 'On '.$files[$key].' the number is'.$row;
}
}
?>
与bash.
首先,创建一些文件:
for n in $(seq 20); do echo $RANDOM > name${n}.txt; done
现在,前 5 名:
$ for f in *; do printf "%s\t%d\n" "$f" $(<$f); done | sort -k2nr | head -n 5
name16.txt 30283
name12.txt 29976
name8.txt 28948
name4.txt 28256
name6.txt 28148
只是文件名
$ for f in *; do printf "%s\t%d\n" "$f" $(<$f); done | sort -k2nr | head -n 5 | cut -f1
name16.txt
name12.txt
name8.txt
name4.txt
name6.txt
grep . name*.txt | sort -nr -k2 | head -n 3
输出(例如):
name4.txt:1
name3.txt:123
name2.txt:444
我有一个包含多个文本文件的目录。 例如:
name1.txt
name2.txt
name3.txt
etc.. etc..
每个文本文件包含 1 行,该行是一个数字,即“10”
我想知道是否有可能以某种方式回显所有文本文件中排名前 10 位的文本文件名。
我想知道这是否可以通过 PHP 实时完成或通过 bash 脚本/cron 定期更新 谢谢!
这不是最有效的想法,但假设您不能使用数据库(否则您可能会):
<?php
$files = scandir('path/to/files/directory');
$rows = Array();
foreach($files as $file){
array_push($rows,file_get_contents('path/to/files/directory/'.$file);
}
arsort($rows);
$i = 0;
foreach($rows as $key => $row){
if($i <= 10){
echo 'On '.$files[$key].' the number is'.$row;
}
}
?>
与bash.
首先,创建一些文件:
for n in $(seq 20); do echo $RANDOM > name${n}.txt; done
现在,前 5 名:
$ for f in *; do printf "%s\t%d\n" "$f" $(<$f); done | sort -k2nr | head -n 5
name16.txt 30283
name12.txt 29976
name8.txt 28948
name4.txt 28256
name6.txt 28148
只是文件名
$ for f in *; do printf "%s\t%d\n" "$f" $(<$f); done | sort -k2nr | head -n 5 | cut -f1
name16.txt
name12.txt
name8.txt
name4.txt
name6.txt
grep . name*.txt | sort -nr -k2 | head -n 3
输出(例如):
name4.txt:1 name3.txt:123 name2.txt:444