sed queation - remove all characters after a hyphen

  • Follow


I think 'sed' is the tool for this, though I may be wrong.

I have a list of files

atlas-3.8.3.p12.spkg
blas-20070724.spkg
boehm_gc-7.1.p5.spkg
boost-cropped-1.34.1.spkg
cddlib-094f.p6.spkg
cephes-2.8.spkg
cliquer-1.2.p5.spkg
conway_polynomials-0.2.spkg
cvxopt-0.9.p8.spkg
cython-0.12.1.spkg
deps
docutils-0.5.p0.spkg
ecl-10.2.1.spkg
eclib-20080310.p10.spkg


and would like to remove the hyphen and all characters after them. So
I get

atlas
blas
boehm_gc
boost-cropped
cddlib
cephes
cliquer
conway_polynomials

etc

What's the best way to do this?

I note 'deps' has no hypen, but I think that is the only such case and
can be handled manually if need be. There's about 100 of these, so
whilst doing them manually is not impossible, it's a bit tedious.

Note I actually want to remove the hyphen, so the subject line is
slightly inaccurate, but any attempt I could think of to rewrite the
subject line in a more accurate form just got too wordy. In any case,
I know how to remove a hyphen easily.

Dave
0
Reply David 6/7/2010 7:47:59 AM

David Kirkby wrote:

> I think 'sed' is the tool for this, though I may be wrong.
> 
> I have a list of files
> 
> atlas-3.8.3.p12.spkg
> blas-20070724.spkg
> boehm_gc-7.1.p5.spkg
> boost-cropped-1.34.1.spkg
> cddlib-094f.p6.spkg
> cephes-2.8.spkg
> cliquer-1.2.p5.spkg
> conway_polynomials-0.2.spkg
> cvxopt-0.9.p8.spkg
> cython-0.12.1.spkg
> deps
> docutils-0.5.p0.spkg
> ecl-10.2.1.spkg
> eclib-20080310.p10.spkg
> 
> 
> and would like to remove the hyphen and all characters after them. So
> I get
> 
> atlas
> blas
> boehm_gc
> boost-cropped
> cddlib
> cephes
> cliquer
> conway_polynomials
> 
> etc
> 
> What's the best way to do this?
> 
> I note 'deps' has no hypen, but I think that is the only such case and
> can be handled manually if need be. There's about 100 of these, so
> whilst doing them manually is not impossible, it's a bit tedious.
> 
> Note I actually want to remove the hyphen, so the subject line is
> slightly inaccurate, but any attempt I could think of to rewrite the
> subject line in a more accurate form just got too wordy. In any case,
> I know how to remove a hyphen easily.

Assuming you mean you want to remove the *last* hypen (which still may not 
be appropriate if you have things like "foo-bar-1.4.5-19.spkg"), try

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

0
Reply pk 6/7/2010 8:08:16 AM


David Kirkby wrote:

> I think 'sed' is the tool for this, though I may be wrong.
> 
> I have a list of files

Do you mean you have a set of files in a directory
> 
> atlas-3.8.3.p12.spkg
> blas-20070724.spkg
> boehm_gc-7.1.p5.spkg
> boost-cropped-1.34.1.spkg
> cddlib-094f.p6.spkg
> cephes-2.8.spkg
> cliquer-1.2.p5.spkg
> conway_polynomials-0.2.spkg
> cvxopt-0.9.p8.spkg
> cython-0.12.1.spkg
> deps
> docutils-0.5.p0.spkg
> ecl-10.2.1.spkg
> eclib-20080310.p10.spkg
> 
> 
> and would like to remove the hyphen and all characters after them. So
> I get

and you want to rename them?
> 
> atlas
> blas
> boehm_gc
> boost-cropped
> cddlib
> cephes
> cliquer
> conway_polynomials
> 
> etc
> 
> What's the best way to do this?

for f in *-*
do
   mv "$f" "${f%-*}"
done

caveat: DON'T have files with names starting with a hyphen.
> 
> I note 'deps' has no hypen, but I think that is the only such case and
> can be handled manually if need be. There's about 100 of these, so
> whilst doing them manually is not impossible, it's a bit tedious.

deps would be left alone with the above solution.

> 
> Note I actually want to remove the hyphen, so the subject line is
> slightly inaccurate, but any attempt I could think of to rewrite the
> subject line in a more accurate form just got too wordy. In any case,
> I know how to remove a hyphen easily.

So you want blas-20070724.spkg -> blas20070724.spkg?

mv "$f" "${f%-*}${f#*-}"

This would mess up names with two hyphens, eg a-b-c -> abbc

Or blas-20070724.spkg -> blas.spkg?

mv "$f" "${f%-*}.${f#*.}"
> 
> Dave

Check out the Parameter Expansion section of your bash man page to see what
is going on.

Andrew
0
Reply Andrew 6/7/2010 9:42:44 AM

On Jun 7, 12:47=A0am, David Kirkby <drkir...@gmail.com> wrote:

> I have a list of files
[...]
> and would like to remove the hyphen and all characters after them.
[...]

sed -e 's/-.*//'

1
Reply Harry 6/7/2010 7:38:05 PM

On Jun 7, 10:42=A0am, Andrew McDermott <a.p.mcderm...@NOSPAM-rl.ac.uk>
wrote:
> David Kirkby wrote:
> > I think 'sed' is the tool for this, though I may be wrong.
>
> > I have a list of files
>
> Do you mean you have a set of files in a directory
>
>
>
>
>
> > atlas-3.8.3.p12.spkg
> > blas-20070724.spkg
> > boehm_gc-7.1.p5.spkg
> > boost-cropped-1.34.1.spkg

>
> > and would like to remove the hyphen and all characters after them. So
> > I get
>
> and you want to rename them?

No, I do not want to rename them.  I just want a list, without the
version numbers. (These are mathematical packages with various version
numbers. I just want a list of the packages.

0
Reply David 6/7/2010 11:33:05 PM

On Jun 7, 8:38=A0pm, Harry <harryooopot...@hotmail.com> wrote:
> On Jun 7, 12:47=A0am, David Kirkby <drkir...@gmail.com> wrote:
>
> > I have a list of files
> [...]
> > and would like to remove the hyphen and all characters after them.
>
> [...]
>
> sed -e 's/-.*//'

Thank you - that is just what I wanted.

Dave
0
Reply David 6/7/2010 11:45:34 PM

David Kirkby wrote:
> On Jun 7, 10:42 am, Andrew McDermott <a.p.mcderm...@NOSPAM-rl.ac.uk>
> wrote:
>> David Kirkby wrote:
>>> I think 'sed' is the tool for this, though I may be wrong.
>>> I have a list of files
>> Do you mean you have a set of files in a directory
>>
>>
>>
>>
>>
>>> atlas-3.8.3.p12.spkg
>>> blas-20070724.spkg
>>> boehm_gc-7.1.p5.spkg
>>> boost-cropped-1.34.1.spkg
> 
>>> and would like to remove the hyphen and all characters after them. So
>>> I get
>> and you want to rename them?
> 
> No, I do not want to rename them.  I just want a list, without the
> version numbers. (These are mathematical packages with various version
> numbers. I just want a list of the packages.
> 

Then pipe your ls output[*] into the sed command that pk suggested.

[*] Of course you don't need ls(1), you can as well use printf (which
is often a shell builtin) and avoid the extra process.

Janis
0
Reply Janis 6/7/2010 11:51:29 PM

David Kirkby wrote:
> On Jun 7, 8:38 pm, Harry <harryooopot...@hotmail.com> wrote:
>> On Jun 7, 12:47 am, David Kirkby <drkir...@gmail.com> wrote:
>>
>>> I have a list of files
>> [...]
>>> and would like to remove the hyphen and all characters after them.
>> [...]
>>
>> sed -e 's/-.*//'
> 
> Thank you - that is just what I wanted.

No, it's not. See what you asked for and look at the /boost/ entry.
Instead take pk's suggestion. (Or are there new requirements now?)

Janis

> 
> Dave
0
Reply Janis 6/7/2010 11:54:18 PM

On Jun 7, 2:47=A0am, David Kirkby <drkir...@gmail.com> wrote:
> I think 'sed' is the tool for this, though I may be wrong.
>
> I have a list of files
>
> atlas-3.8.3.p12.spkg
> blas-20070724.spkg
> boehm_gc-7.1.p5.spkg
> boost-cropped-1.34.1.spkg
> cddlib-094f.p6.spkg
> cephes-2.8.spkg
> cliquer-1.2.p5.spkg
> conway_polynomials-0.2.spkg
> cvxopt-0.9.p8.spkg
> cython-0.12.1.spkg
> deps
> docutils-0.5.p0.spkg
> ecl-10.2.1.spkg
> eclib-20080310.p10.spkg
>
> and would like to remove the hyphen and all characters after them. So
> I get
>
> atlas
> blas
> boehm_gc
> boost-cropped
> cddlib
> cephes
> cliquer
> conway_polynomials
>
> etc
>
> What's the best way to do this?
>
> I note 'deps' has no hypen, but I think that is the only such case and
> can be handled manually if need be. There's about 100 of these, so
> whilst doing them manually is not impossible, it's a bit tedious.
>
> Note I actually want to remove the hyphen, so the subject line is
> slightly inaccurate, but any attempt I could think of to rewrite the
> subject line in a more accurate form just got too wordy. In any case,
> I know how to remove a hyphen easily.
>
> Dave

You could also use awk pretty efficiently:

awk 'BEGIN {FS=3DOFS=3D"-"} {print $1}' [YOURFILELIST]
0
Reply Fostytou 6/21/2010 8:03:02 PM

On Jun 21, 3:03=A0pm, Fostytou - AKA FrostFace <fosty...@gmail.com>
wrote:
> On Jun 7, 2:47=A0am, David Kirkby <drkir...@gmail.com> wrote:
>
>
>
>
>
> > I think 'sed' is the tool for this, though I may be wrong.
>
> > I have a list of files
>
> > atlas-3.8.3.p12.spkg
> > blas-20070724.spkg
> > boehm_gc-7.1.p5.spkg
> > boost-cropped-1.34.1.spkg
> > cddlib-094f.p6.spkg
> > cephes-2.8.spkg
> > cliquer-1.2.p5.spkg
> > conway_polynomials-0.2.spkg
> > cvxopt-0.9.p8.spkg
> > cython-0.12.1.spkg
> > deps
> > docutils-0.5.p0.spkg
> > ecl-10.2.1.spkg
> > eclib-20080310.p10.spkg
>
> > and would like to remove the hyphen and all characters after them. So
> > I get
>
> > atlas
> > blas
> > boehm_gc
> > boost-cropped
> > cddlib
> > cephes
> > cliquer
> > conway_polynomials
>
> > etc
>
> > What's the best way to do this?
>
> > I note 'deps' has no hypen, but I think that is the only such case and
> > can be handled manually if need be. There's about 100 of these, so
> > whilst doing them manually is not impossible, it's a bit tedious.
>
> > Note I actually want to remove the hyphen, so the subject line is
> > slightly inaccurate, but any attempt I could think of to rewrite the
> > subject line in a more accurate form just got too wordy. In any case,
> > I know how to remove a hyphen easily.
>
> > Dave
>
> You could also use awk pretty efficiently:
>
> awk 'BEGIN {FS=3DOFS=3D"-"} {print $1}' [YOURFILELIST]- Hide quoted text =
-
>
> - Show quoted text -

No need to assign OFS if you're only printing 1 field:

awk -F- '{print $1}' files

     Ed.
0
Reply Ed 6/21/2010 8:54:03 PM

On Jun 7, 12:47=A0am, David Kirkby <drkirkby@gmail.com> wrote:
> I think 'sed' is the tool for this, though I may be wrong.
>
> I have a list of files
>
> atlas-3.8.3.p12.spkg
> blas-20070724.spkg
> boehm_gc-7.1.p5.spkg
> boost-cropped-1.34.1.spkg
> cddlib-094f.p6.spkg
> cephes-2.8.spkg
> cliquer-1.2.p5.spkg
> conway_polynomials-0.2.spkg
> cvxopt-0.9.p8.spkg
> cython-0.12.1.spkg
> deps
> docutils-0.5.p0.spkg
> ecl-10.2.1.spkg
> eclib-20080310.p10.spkg
>
> and would like to remove the hyphen and all characters after them. So
> I get
>
> atlas
> blas
> boehm_gc
> boost-cropped
> cddlib
> cephes
> cliquer
> conway_polynomials
>
> etc
>
> What's the best way to do this?
>
> I note 'deps' has no hypen, but I think that is the only such case and
> can be handled manually if need be. There's about 100 of these, so
> whilst doing them manually is not impossible, it's a bit tedious.
>
> Note I actually want to remove the hyphen, so the subject line is
> slightly inaccurate, but any attempt I could think of to rewrite the
> subject line in a more accurate form just got too wordy. In any case,
> I know how to remove a hyphen easily.

sed -e 's/-.*$//'
0
Reply Michael 6/25/2010 4:55:06 AM

On Jun 7, 12:47=A0pm, David Kirkby <drkir...@gmail.com> wrote:
> I think 'sed' is the tool for this, though I may be wrong.
>
> I have a list of files
>
> atlas-3.8.3.p12.spkg
> blas-20070724.spkg
> boehm_gc-7.1.p5.spkg
> boost-cropped-1.34.1.spkg
> cddlib-094f.p6.spkg
> cephes-2.8.spkg
> cliquer-1.2.p5.spkg
> conway_polynomials-0.2.spkg
> cvxopt-0.9.p8.spkg
> cython-0.12.1.spkg
> deps
> docutils-0.5.p0.spkg
> ecl-10.2.1.spkg
> eclib-20080310.p10.spkg
>
> and would like to remove the hyphen and all characters after them. So
> I get
>
> atlas
> blas
> boehm_gc
> boost-cropped
> cddlib
> cephes
> cliquer
> conway_polynomials
>
> etc
>
> What's the best way to do this?
>
> I note 'deps' has no hypen, but I think that is the only such case and
> can be handled manually if need be. There's about 100 of these, so
> whilst doing them manually is not impossible, it's a bit tedious.
>
> Note I actually want to remove the hyphen, so the subject line is
> slightly inaccurate, but any attempt I could think of to rewrite the
> subject line in a more accurate form just got too wordy. In any case,
> I know how to remove a hyphen easily.
>
> Dave

cut -c'-' -f1
0
Reply Rakesh 6/25/2010 11:58:21 AM

You don't need sed. bash is enough and fast

for fl in $(ls *spkg);do
  echo ${fl%%-*}
done

0
Reply hehe2046 (7) 8/2/2010 7:58:44 AM

On 02.08.2010 09:58, hehe2046 wrote:
> You don't need sed. bash is enough and fast
>
> for fl in $(ls *spkg);do
>    echo ${fl%%-*}
> done

no need for the error prone ls subshell.
'%%-*' removes all hyphens. that would trim boost-cropped-1.34.1.spkg 
too far.

cd /whatever/dir
for x in *; do printf "%s\n" "${x%-*}"; done

there's one downside i know of - if the directory does not contain any 
files, the globbing pattern is shown (afaik sh, bash, ksh, more?):

cd /tmp/t/
# for x in *; do printf "%s\n" "$x"; done
*
#

one might 'test -[e|f]' to work around, but that slows down the whole thing.

best regards

mart
0
Reply Mart 8/2/2010 9:46:26 AM

On 02.08.2010 11:46, Mart Frauenlob wrote:
[...]

> cd /whatever/dir
> for x in *; do printf "%s\n" "${x%-*}"; done
>
> there's one downside i know of - if the directory does not contain any
> files, the globbing pattern is shown (afaik sh, bash, ksh, more?):

I should have said, 'not any matching files'...
>
> cd /tmp/t/
> # for x in *; do printf "%s\n" "$x"; done
> *
> #
>
also to mention that it'll also list directories...

> one might 'test -[e|f]' to work around, but that slows down the whole
> thing.
>


0
Reply Mart 8/2/2010 11:32:10 AM

14 Replies
9765 Views

(page loaded in 0.313 seconds)

Similiar Articles:


















7/19/2012 3:11:50 PM


Reply: