I am trying to clean a list containing packages. I wish to remove the package revisions, leaving the package names only.

I see every package has naming convention like this: package-name-majorver-minorver , etc: openssl-1.0.1e-57.0.5.el6 . I want openssl only, not -1.0.1e-57.0.5.el6

So far I have this sed 's/^.*-//', but this doesn't work obviously.

  • watch out for package names with dashes! If you're gathering the list with RPM, consider using the --queryformat to get the %NAME separately. – Jeff Schaller Mar 13 at 11:05

If the package name always has two version number parts appended, separated by hyphens, and if the version parts never contain a hyphen, you can use this command

sed 's/-[^-]*-[^-]*$//'

This will remove two hyphens, each followed by 0 or more non-hyphen characters, at the end of the line ($). It would also change e.g. foo-bar-baz-- to foo-bar-baz.


If the package name doesn't contain -, this should work:

sed -r 's/([^-]+).*/\1/' 
  • unfortunately, it does, and the number of - 's are inconsistent too, eg: tmux and python-pip, the only consistent ones are major ver and minor ver appended to back separated by - – user121392 Mar 13 at 10:10

Well I know it's not the best solution, but I found this https://stackoverflow.com/a/51153277/5227747, so the solution was

cat file | sed 's/ // ; s/-/\t/' | rev | cut -f 3- | rev | sed 's/\t/-/'

it removes the last two '-'-separated columns without fuss, if anyone knows how to do it better I would love to know.

  • Unneccessary cat – Weijun Zhou Mar 13 at 10:57
  • I have to read from a file. I will check sed to improve the command – user121392 Mar 13 at 11:49
  • You should almost always do sed ... file instead of cat file | sed ... – Weijun Zhou Mar 13 at 22:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.