开发者

How can I remove the timestamp from a filename in Perl?

开发者 https://www.devze.com 2022-12-20 12:36 出处:网络
I have a file which has a line in it as: /hosting/logs/U01开发者_StackOverflow中文版-ecom-SIT01/CU01-DC05-IFIO_SIT01_NU01-nc3sz1ecmas11/waslogs/SystemOut_10.01.21_16.54.18.log`

I have a file which has a line in it as:

 /hosting/logs/U01开发者_StackOverflow中文版-ecom-SIT01/CU01-DC05-IFIO_SIT01_NU01-nc3sz1ecmas11/waslogs/SystemOut_10.01.21_16.54.18.log`

I need a script which would read this line and remove the time stamp from it, that is:

 10.01.21_16.54.18

The script should print the filename without the timestamp and holding the full path, that is:

 /hosting/logs/U01-ecom-SIT01/CU01-DC05-IFIO_SIT01_NU01-nc3sz1ecmas11/waslogs/SystemOut.log`

Please help as I'm unable to pattern match and output the file path without the timestamp.


echo "/hosting/logs/U01-ecom-SIT01/CU01-DC05-IFIO_SIT01_NU01-nc3sz1ecmas11/waslogs/SystemOut_10.01.21_16.54.18.log" | 
  perl -pe "s/_\d\d\.\d\d\.\d\d_\d\d\.\d\d\.\d\d//;"


$ perl -e 's{_\d{2}\.\d{2}.\d{2}_\d{2}\.\d{2}.\d{2}}{} and print for @ARGV' /hosting/logs/U01-ecom-SIT01/CU01-DC05-IFIO_SIT01_NU01-nc3sz1ecmas11/waslogs/SystemOut_10.01.21_16.54.18.log


Path shortened to prevent scrolling:

$ cat paths
CU01-DC05-IFIO_SIT01_NU01-nc3sz1ecmas11/waslogs/SystemOut_10.01.21_16.54.18.log

$ perl -pe 's/(_(\d\d(\.\d\d){2})){2}\.log$/.log/' paths
CU01-DC05-IFIO_SIT01_NU01-nc3sz1ecmas11/waslogs/SystemOut.log

The timestamp is made up of 2 sequences that look like _##.##.##. The subsequences end with 2 sequences of .##. These are the roles of the {2} quantifiers.


while(<>){
    @s = split /\// ;
    $fullpath=join("/",splice @s , 0, $#s);
    @a = split /[_.]/ ,$s[-1];
    $newfile="$fullpath/$a[0].$a[-1]";
    print $newfile."\n";
}


You can use the following coding

 use strict;
    use warnings;
  my $var; $var=/hosting/logs/U01-ecom-SIT01/CU01-DC05-IFIO_SIT01_NU01-nc3sz1ecmas11/waslogs/SystemOut_10.01.21_16.54.18.log";
    $var=~s/_\d\d\.\d\d\.\d\d//g;
    # $var=~s/_10\.01\.21_16\.54\.18//g; # You can use this way also 
    print "$var\n";
0

精彩评论

暂无评论...
验证码 换一张
取 消