2

I would like to grab the date portion out of file paths so I may use them in a file name. For example, the files may be of the format:

$file1 = "/mypath/sd-urt7-dfc-adfj345h-d0-79week48a-DFC-lk-my.text"  
$file2 = "/mypath/sd-urt7-afd-parent-79week46d-AFD-lk-my.text"  
$file3 = "/mypath/sd-urt7-ert-parent-79week50c-ERT-lk-my.text"  

Regardless of the file name formats from above, there is a portion in there that has the format "79weekxxX" where xx is a week number and X is a letter. I want to grab the portion "weekxxX" from each of these string names in order to create a final file name with just this string. For example, if my file is $file1, then I would like to store it in another variable such as week which contains week48a and create another variable just called $filename="$week.txt".

I'm thinking regex matching can be used to do this but I'm not quite sure how. Please let me know if any part of my question remains unclear so that I may further elaborate. Thank you.

Edit: The "79week" is always there. To be exact, I will not be accessing them as variables with file names. The paths are values of a nested hash for its own corresponding keys. So would it work to do something like:

 my %hash = ( 
        "rev" => {
            "first" => "$filepath1",  
            "second" => "$filepath2", 
            "third" => "$filepath3"                      
        },  
        "for" => {
            "first" => "$filepath1_2",  
            "second" => "$filepath2_2", 
            "third" => "$filepath3_2" 
        }   
    );

foreach my $inner (keys %{$hash{$outer}}){
my $file = basename($inner); //only takes the file name, and ignores the rest of the path
my ($week) = $file =~ /79week(\d\d)([a-zA-Z])/;
$filename = "$week.txt";
}
3
  • Is the 79 and week always there? If so, fairly easy to do /79week(\d\d)([a-zA-Z])/ Demo Commented Dec 21, 2019 at 20:26
  • @dawg please check post edit Commented Dec 21, 2019 at 20:57
  • my ($week) = $file1 =~ /-[0-9]{2}(week[0-9]{2}[a-zA-Z])-/; Commented Dec 21, 2019 at 20:57

1 Answer 1

1

General Observation:

If $file contains a string such as:

/mypath/sd-urt7-dfc-adfj345h-d0-79week48a-DFC-lk-my.text,

then if you execute:

$file =~ /79(week\d+[a-z])/i;

variable $1, which is set to the Group 1 match from the above regex, will contain (in the above example):

week48a

You can then assign my $week = $1;

I hope that answers you Regex-related question.

Your statement:

my ($week) = $file =~ /79week(\d\d)([a-zA-Z])/;

will only set $week to Group 1, or 48. So the other alternative, would be:

my ($week) = $file =~ /79(week\d\d[a-zA-Z])/;

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.