You are not logged in.

#1 2010-11-28 21:40:30

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,672
Website

sed help - delete eveything from a character to end of line [solved]

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 smile

$ 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 packagesZsh and other configs

Offline

#2 2010-11-28 21:58:50

Gusar
Member
Registered: 2009-08-25
Posts: 3,605

Re: sed help - delete eveything from a character to end of line [solved]

$ ls *.pkg.tar.{x,g}z | sed 's/-.*//g'

Offline

#3 2010-11-28 21:59:38

Runiq
Member
From: Germany
Registered: 2008-10-29
Posts: 1,053

Re: sed help - delete eveything from a character to end of line [solved]

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

Last edited by Runiq (2010-11-28 22:07:43)

Offline

#4 2010-11-28 22:04:22

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: sed help - delete eveything from a character to end of line [solved]

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

#5 2010-11-28 22:11:44

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,672
Website

Re: sed help - delete eveything from a character to end of line [solved]

@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 smile

Last edited by graysky (2010-11-28 22:13:05)


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#6 2010-11-28 22:15:23

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,672
Website

Re: sed help - delete eveything from a character to end of line [solved]

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 packagesZsh and other configs

Offline

#7 2010-11-28 22:17:33

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: sed help - delete eveything from a character to end of line [solved]

so go more aggressive...

for pkg in *.pkg.tar.{x,g}z; do
  echo ${pkg%-*-*-*}
done
graysky wrote:

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

#8 2010-11-28 22:33:35

Gusar
Member
Registered: 2009-08-25
Posts: 3,605

Re: sed help - delete eveything from a character to end of line [solved]

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

#9 2010-11-28 23:16:14

Runiq
Member
From: Germany
Registered: 2008-10-29
Posts: 1,053

Re: sed help - delete eveything from a character to end of line [solved]

Gusar wrote:

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

Last edited by Runiq (2010-11-28 23:23:00)

Offline

Board footer

Powered by FluxBB