Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在bash中找到最新的匹配模式文件_Bash - Fatal编程技术网

如何在bash中找到最新的匹配模式文件

如何在bash中找到最新的匹配模式文件,bash,Bash,我试图只复制Bash中最新版本的文件 例如,在我下面的脚本中,我正在复制所有文件,但现在我需要复制最新版本(最新版本将作为文件名中的最后一个参数提供) 我的文件名示例: AAA_BBB_CCC_1 AAA_BBB_CCC_2 # I need to copy this file instead the above one because it has # _2 which means it is the latest version. BBB_CCC_DDD_1

我试图只复制Bash中最新版本的文件

例如,在我下面的脚本中,我正在复制所有文件,但现在我需要复制最新版本(最新版本将作为文件名中的最后一个参数提供)

我的文件名示例:

AAA_BBB_CCC_1
AAA_BBB_CCC_2  # I need to copy this file instead the above one because it has
               # _2 which means it is the latest version.

BBB_CCC_DDD_1
BBB_CCC_DDD_2  # I need to copy this file

我很懒,所以我会用Perl来完成这个任务。根据“版本为下划线,名称末尾为数字”进行操作,您可以使用它读取每行一个文件作为输入,并在输出中生成每个文件的最新版本:

#!/usr/bin/env perl
use strict;
use warnings;

my %files;

while (<>)
{
    chomp;
    my($base, $vrsn) = m/(.*)_(\d+)$/;
    $files{$base} //= $vrsn;    # Set if not yet defined
    $files{$base}   = $vrsn if ($vrsn > $files{$base});
}

foreach my $base (sort keys %files)
{
    print "${base}_$files{$base}\n";
}
#/usr/bin/env perl
严格使用;
使用警告;
我的%s文件;
而()
{
咀嚼;
我的($base,$vrsn)=m/(.*)u(\ d+)$/;
$files{$base}/=$vrsn;#如果尚未定义,则设置
$files{$base}=$vrsn if($vrsn>$files{$base});
}
foreach my$base(对密钥%文件进行排序)
{
打印“${base}{$files{$base}\n”;
}

您可以在任何需要的地方将其安装到管道中。

大约有一百万个工具可以处理版本化文件或智能目录结构更新,为什么不使用其中的一个呢?(一个版本控制系统,
rsync
…)非常感谢您将尝试在我的应用程序中安装此功能
$base
是名称的版本号之前的部分,是名称的基本部分
$vrsn
是版本号,位于最后一个下划线之后。使用您的
AAA_BBB_CCC_1
示例名称,
($base eq“AAA_BBB_CCC”&&$vrsn eq“1”)
计算结果为true。