How to remove a timestamp from a file name in Perl?

I have a file that has a line in it:

/hosting/logs/U01-ecom-SIT01/CU01-DC05-IFIO_SIT01_NU01-nc3sz1ecmas11/waslogs/SystemOut_10.01.21_16.54.18.log` 

I need a script that would read this line and delete the timestamp, namely:

  10.01.21_16.54.18 

The script should print the file name without a timestamp and hold the full path, namely:

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

Please help, as I cannot match the image and print the file path without a timestamp.

+4
source share
5 answers
 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//;" 
+1
source

$ 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

+1
source

Shortened path 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 consists of two sequences that look like _##.##.## . Subsequences end with 2 sequences .## . These are the roles of quantifiers {2} .

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

You can use the following encoding

  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
source

Source: https://habr.com/ru/post/1301815/


All Articles