You are not logged in.
I'd like to take the following listing and use sed to strip out everything after the hyphen-# so I get just the names of my packages.
$ ls *.pkg.tar.{x,g}z
xproto-7.0.19-1-any.pkg.tar.xz
xterm-266-1-x86_64.pkg.tar.xz
xvidcap-1.1.7-3-x86_64.pkg.tar.xz
xvidcore-1.2.2-1-x86_64.pkg.tar.xz
xz-5.0.0-1-x86_64.pkg.tar.gz
zenity-2.32.1-1-x86_64.pkg.tar.xz
zip-3.0-1-x86_64.pkg.tar.xz
zlib-1.2.5-3-x86_64.pkg.tar.xz
I've been playing around with it but have yet to do it
$ ls *.pkg.tar.{x,g}z | sed 's/-[0-9]$//g'
That doesn't work.
Last edited by graysky (2010-11-28 22:17:04)
CPU-optimized Linux-ck packages @ Repo-ck • AUR packages • Zsh and other configs
Offline
$ ls *.pkg.tar.{x,g}z | sed 's/-.*//g'
Offline
ls | sed 's/-.*$//g'
works for me.
Edit: No, it doesn't—cuts off kernel-headers too early, for example. Better answer falconindy's questions first.
Last edited by Runiq (2010-11-28 22:07:43)
Offline
Does it have to involve sed? I'm not exactly clear on what you want your output to look like, but...
for pkg in *.pkg.tar.{x,g}z; do
echo ${pkg%-*}
done
From an input file...
while read pkg; do
echo ${pkg%-*}
done < inputfile
Or if you're working with the name of every installed package...
pacman -Q | tr ' ' '-'
FYI: the g option to the s operator in sed means greedy. You don't want that when you're only replacing one instance of the pattern. If you really wanted to do it with sed, use the pattern: s/-[^-]\+$//
edit: yikes, too slow.
Last edited by falconindy (2010-11-28 22:05:16)
Offline
@Gusar and Runiq and falconindy - thanks for the suggestions, but none of the sed lines seem to hit what I need them to hit... basically, the cutpoint of the sed statement needs to be the -#
For example, I need to turn this into this:
xorg-xsetroot-1.1.0-1-x86_64.pkg.tar.xz --> xorg-xsetroot
The output is not out of pacman - I have compiled some packages via abs and I want to compare them to my pacman list of installed packages which is why I need to trim them down from long filenames. I want to use sed because I have needed to do this sort of thing before with sed but never got it quite right
Last edited by graysky (2010-11-28 22:13:05)
CPU-optimized Linux-ck packages @ Repo-ck • AUR packages • Zsh and other configs
Offline
I think I got it!
ls *.pkg.tar.{x,g}z | sed 's/-[0-9].*$//g'
The magic seems to be that little dot before the * which I don't understand! Anyway, thanks all!
Last edited by graysky (2010-11-28 22:16:45)
CPU-optimized Linux-ck packages @ Repo-ck • AUR packages • Zsh and other configs
Offline
so go more aggressive...
for pkg in *.pkg.tar.{x,g}z; do
echo ${pkg%-*-*-*}
done
The magic seems to be that little dot before the * which I don't understand!
Globbing and regex wildcards are not the same. Regex has the ability to be far more precise.
Globbing: * matches anything, zero or more times.
Regex: .* matches any character (the dot), zero or more times (the star).
The different is that you're specifically asking the regex to match anything, as opposed to a character range or a specific char.
tl;dr: sed doesn't do globs.
Last edited by falconindy (2010-11-28 22:21:08)
Offline
Bah, I did have -[0-9].* at first, don't know why I changed it. Though this one assumes the version always starts with a number, is this always the case?
I see falconindy already explained about the dot. So I'll just add that the $ is redundant in this case. I doesn't hurt being there, but it does nothing. You use $ when it's the end of the line you're interested in, like say in a scenario where you want to remove all lines that end in "blah", you'd use '/blah$/d'
Offline
Bah, I did have -[0-9].* at first, don't know why I changed it. Though this one assumes the version always starts with a number, is this always the case?
Nope, but that's not the only problem—there are also packages whose names end with a hyphen and a number:
$ pacman -Ssq | grep '.*-[0-9]'
gecko-sharp-2.0
gtk-sharp-2
gtksourceview-sharp-2.0
ntfs-3g
nvidia-173xx
nvidia-173xx-utils
nvidia-96xx
nvidia-96xx-utils
ttf-mph-2b-damase
xorg-fonts-100dpi
xorg-fonts-75dpi
celt-0.7
gl-117
spring-1944
Example for gl-117:
$ touch gl-117-1.3.2-4-x86_64.pkg.tar.xz
$ ls *.pkg.tar.{x,g}z | sed 's/-[0-9].*$//'
gl
So the -117 string, which is still part of the package name, gets cut off. I think you'll have to take into consideration that there are always 3 hyphens from the end of the string to the package name: wine—1.3.8—1—x86_64.pkg.tar.xz, for example. I got it right with the following sed command:
$ ls | sed 's/\(.*\)-.*-[0-9]*-.*$/\1/' # the [0-9] in the middle is for $pkgrel, which is always a number of digits
gl-117 # just as it should
Edit: Better use falconindy's globbing method. Looks a lot cleaner.
Last edited by Runiq (2010-11-28 23:23:00)
Offline