How to remove text after '-'?

I have a list of files (basically they are .deb packages). Let's say:

abc-de-1.2.3-1.deb
fgh-ij-4.5.6-2.deb
klm-no-7.8.9-3.deb
pqrs-10.11.12-4.deb
...

As you can see some of the file names have numbers after a - while others have some text after a - and then numbers after the next -.

Are there any ways to remove everything starting from the numbers including the -, i.e.,

abc-de
fgh-ij
klm-no
pqrs
...

I want to edit the list, not rename the files.


Solution 1:

If you're able to use the first number to identify what you want to remove every time, you could use:

$ sed 's/-[0-9].*//' file
abc-de
fgh-ij
klm-no
pqrs

Notes

  • s/old/new/ replace old with new
  • [0-9] some digit
  • .* any number of any characters

Solution 2:

Using grep with Perl regular expressions:

$ grep -Po "^[a-z-]*(?=-[0-9])" filename
abc-de
fgh-ij
klm-no
pqrs