0

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

FieldyScop
  • 39
  • 4
  • There are many ways to achieve that - what have you tried? – tink Nov 01 '22 at 01:41
  • I didn't try much because I don't really know how. I tried this ``` name1=$(echo Huistaak1-HelloWorld_Jan.Janssens.s.ua_poging_2019-11-09.tgz | cut -f2 -d '_') mkdir -p "$name1" ``` but then I would have to do it for every single tgz file and I don't get the result I need – FieldyScop Nov 01 '22 at 01:45

1 Answers1

2

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

Cyrus
  • 84,225
  • 14
  • 89
  • 153
  • Can you explain what this does ? [[ $i =~ ([^_.]+)\.([^_]+)\.s ]] I know it's regex but I don't understand how you got it – FieldyScop Nov 01 '22 at 02:24
  • 1
    @FieldyScop: I've updated my answer. I hope it became more understandable. – Cyrus Nov 01 '22 at 12:05