Search code examples
awkseddelimitercut

Filtering a list based on sed and awk command, multiple delimiter


I need help in this issue. I have some files in a folder and I want to list it, but I want to list only the main Cours-name.

-r--r----- 1 saeid azubi 0 Jun 10 14:34 'Gymnastik - Bauch-Beine-Po 01.mp4'
-r--r----- 1 saeid azubi 0 Jun 10 14:34 'Gymnastik - Bauch-Beine-Po 02.mp4'
-r--r----- 1 saeid azubi 0 Jun 10 14:34 'Gymnastik - Bauch-Beine-Po 03.mp4'
-r--r----- 1 saeid azubi 0 Jun 10 14:34 'Gymnastik - Bauch-Beine-Po 04.mp4'
-r--r----- 1 saeid azubi 0 Jun 10 14:35 'Gymnastik - Bauch-Beine-Po 05.mp4'
-r--r----- 1 saeid azubi 0 Jun 10 14:35 'Gymnastik - Bauch-Beine-Po 06.mp4'
-r--r----- 1 saeid azubi 0 Jun 10 14:35 'Gymnastik - Bauch-Beine-Po 07.mp4'
-r--r----- 1 saeid azubi 0 Jun 10 14:35 'Gymnastik - Bauch-Beine-Po 08.mp4'
-r--r----- 1 saeid azubi 0 Jun 10 14:35 'Gymnastik - Bauch-Beine-Po 09.mp4'
-r--r----- 1 saeid azubi 0 Jun 10 14:35 'Gymnastik - Bauch-Beine-Po 10.mp4'
-r--r----- 1 saeid azubi 0 Jun 10 14:35 'Gymnastik - Bauch-Beine-Po 11.mp4'
-r--r----- 1 saeid azubi 0 Jun 10 14:35 'Gymnastik - Entspannung.mp4'
-r--r----- 1 saeid azubi 0 Jun 10 14:35 'Gymnastik - Muskeln wie Stahl 01.mp4'
-r--r----- 1 saeid azubi 0 Jun 10 14:35 'Gymnastik - Muskeln wie Stahl 02.mp4'
-r--r----- 1 saeid azubi 0 Jun 10 14:35 'Gymnastik - Muskeln wie Stahl 03.mp4'
-r--r----- 1 saeid azubi 0 Jun 10 14:35 'Gymnastik - Muskeln wie Stahl 04.mp4'
-r--r----- 1 saeid azubi 0 Jun 10 14:35 'Gymnastik - Muskeln wie Stahl 05.mp4'
-r--r----- 1 saeid azubi 0 Jun 10 14:35 'Gymnastik - Muskeln wie Stahl 06.mp4'
-r--r----- 1 saeid azubi 0 Jun 10 14:35 'Gymnastik - Muskeln wie Stahl 07.mp4'
-r--r----- 1 saeid azubi 0 Jun 10 14:34 'Gymnastik - RückenFit 01.mp4'
-r--r----- 1 saeid azubi 0 Jun 10 14:34 'Gymnastik - RückenFit 02.mp4'
-r--r----- 1 saeid azubi 0 Jun 10 14:34 'Gymnastik - RückenFit 03.mp4'
-r--r----- 1 saeid azubi 0 Jun 10 14:34 'Gymnastik - RückenFit 04.mp4'
-r--r----- 1 saeid azubi 0 Jun 10 14:34 'Gymnastik - RückenFit 05.mp4'
-r--r----- 1 saeid azubi 0 Jun 10 14:34 'Gymnastik - Stretching 01.mp4'
-r--r----- 1 saeid azubi 0 Jun 10 14:34 'Gymnastik - Stretching 02.mp4'
-r--r----- 1 saeid azubi 0 Jun 10 14:34 'Gymnastik - Stretching 03.mp4'

here

The output should be like this :

Bauch-Beine-Po

Entspannung

Muskeln wie Stahl

RückenFit

Stretching

I tried this code but its not what exactly what I want:

ls -l | awk -F'[ .\$]' '{print $11 }' | uniq 

Thanks for helping in advance


Solution

  • You might also use awk.

    Use - as the field separator and remove the optional space and digits followed by the extension from the last field.

    For example

    ls -A1 | awk -F ' - ' '{sub(/( [0-9]+)?\.[^.]+$/, "", $NF);print $NF}' | sort | uniq
    

    In the example code:

    • $NF is the last field, which will first be changed by sub and then printed
    • The pattern ( [0-9]+)?\.[^.]+$ matches an optional space and 1+ digits followed by a dot and any char except a dot until the end of the string