Is there a way to create a directory by cutting a tgz file name in sections?

Issue

So I got multiple .tgz files listed here :

Huistaak1-HelloWorld_Jan.Janssens.s.ua_poging_2019-11-09.tgz
Huistaak1-HelloWorld_Jolien.Peters.s.ua_poging_2019-11-11.tgz
Huistaak1-HelloWorld_Jonas.De.Preter.s.ua_poging_2019-11-12.tgz
Huistaak1-HelloWorld_Len.Feremans.s.ua_poging_2019-11-10.tgz
Huistaak1-HelloWorld_Peter.Hofkens.s.ua_poging_2019-11-11.tgz
Huistaak1-HelloWorld_Sarah.Van.Hoof.s.ua_poging_2019-11-11.tgz

So I need to filter out the names from these files and save them into a variable so that I can use that variable to create a directory
Note: I cannot simply use -> mkdir Janssens.Jan

For example the first file has the name – Jan.Janssens
With that name I would need to create a directory called – Janssens.Jan
In total I should have 6 directories like this

Janssens.Jan
Peters.Jolien
De.Preter.Jonas
Feremans.Len
Hofkens.Peter
Van.Hoof.Sarah

Is there a way that I can filter out the name from every file without having to go through each of them ?

Something like for filename in *.tgz; do …

Solution

With bash and a regex. I assume that there is always .s after the names.

for i in *.tgz; do
  [[ $i =~ ([^_.]+)\.([^_]+)\.s ]] && echo "${BASH_REMATCH[2]}.${BASH_REMATCH[1]}";
done

If string $i matches regex [^_.]+\.[^_]+\.s (I remove all round brackets) then print element 2 (${BASH_REMATCH[2]}) from array BASH_REMATCH followed by a . and element 1 (${BASH_REMATCH[1]}) from array BASH_REMATCH. Element 1 contains matching part from regex between first round brackets [^_.]+ and element 2 contains matching part between second round brackets [^_]+. \. represents a . and not a special character of a regex.

Output:

Janssens.Jan
Peters.Jolien
De.Preter.Jonas
Feremans.Len
Hofkens.Peter
Van.Hoof.Sarah

See: The Stack Overflow Regular Expressions FAQ

Answered By – Cyrus

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published