Prev: Example of kill -9 erasing and/or corrupting a (temporary) file?
Next: wget -r (recursive) failing to back up a site
From: David Kirkby on 7 Jun 2010 03:47 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
From: pk on 7 Jun 2010 04:08 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/-[^-]*$//'
From: Andrew McDermott on 7 Jun 2010 05:42 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
From: Harry on 7 Jun 2010 15:38 On Jun 7, 12:47 am, David Kirkby <drkir...(a)gmail.com> wrote: > I have a list of files [...] > and would like to remove the hyphen and all characters after them. [...] sed -e 's/-.*//'
From: David Kirkby on 7 Jun 2010 19:33
On Jun 7, 10:42 am, Andrew McDermott <a.p.mcderm...(a)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. |