gpt4 book ai didi

bash - 使用 bash 在一组目录中获取特定文件类型的最高后缀(或前缀)的最干净的方法?

转载 作者:行者123 更新时间:2023-12-05 05:19:17 32 4
gpt4 key购买 nike

我有一组跨多个目录的数据文件,格式为

ls lcp01/output/
> dst000.dat dst001.dat ... dst075.dat nn000.dat nn001.dat ... nn036.dat aa000.dat aa001.dat ... aa040.dat

也就是说,有一组目录lcp01lcp25,在它们的输出文件夹中有一组不同的数据文件。我想知道每个目录中最大的 dstXXX.dat 文件是多少(在示例中显示的结果是 75)。

我写了一个实现这个的脚本,但我对最后一步有点不满意:

#!/bin/bash

for i in `seq -f "%02g" 1 25`; #specify dir extensions 1 through 25
do
echo " "
echo $i
names=($(ls lcp$i/output | grep dst )) #dir containing dst files

NUMS=()
for j in "${names[@]}";
do
temp="$(echo $j | tr -dc '0-9' && printf " ")" # record suffixes for each dst file
NUMS+=("$((10#$temp))") #force base 10 interpretation of dst suffixes
done

numList="$(echo "${NUMS[*]}" | sort -nr | head -n1)"
echo ${numList:(-3)} #print out the last 3 characters of the sorted list - the largest file suffix
done

最后两步组织输出索引列表,然后我显示该列表的最后 3 个字符,这将是我最大的文件编号(前提是文件编号小于 100)。

有没有更简洁的方法来做到这一点?理想情况下,我希望更多地控制输出格式,但主要是读出最后 3 个字符的步骤。我希望能够只输出最大的数字,这应该是列表的最后一个元素,但我不知道如何做。

最佳答案

您可以执行以下操作:

for d in lc[0-9][0-9]; do find $d -name 'dst*.dat' -print | sort -u | tail -n1; done

以上命令仅在编号具有相同位数(dst001..999.dat)时有效,因为它被排序为字符串;如果不是这样:

for d in lc[0-9][0-9]; do echo -n $d: ; find $d -name 'dst*.dat' -print | grep -o '[0-9]*.dat' | sort -n | tail -n1; done

关于bash - 使用 bash 在一组目录中获取特定文件类型的最高后缀(或前缀)的最干净的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46643773/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com