Home > Software engineering >  Is there a way to create a directory by cutting a tgz file name in sections?
Is there a way to create a directory by cutting a tgz file name in sections?

Time:11-01

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 ...

CodePudding user response:

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

Output:

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

See: The Stack Overflow Regular Expressions FAQ

  • Related