I am taking a shell scripting class, bu the prof fires off info at us
for about an hour and then leaves campus. We're supposed to be in lab
for two hours, but he's gone and the proctor won't help.
My problem is this: I need to write a ksh script called "unsuffix" that
will take two arguments, the first being a filename and the second being
the extension the user wants to strip from the filename. So if the user
types
unsuffix report.old .old
he will remove the .old extension and the user will be left with a file
called "report."
I haven't a clue how to do it. I started with cut, and crapped out. He
said we woulf be able to do it with sed, but I don't know how.
Any help greatly appreciated.
Thomas
A command exists whose purpose is exactly what you want : basename
--
Herve Couppe de Lahongrais (SEU) | Eurocontrol Experimental Centre
mailto:c...@eurocontrol.fr | France
Just think about what you need to do:
You need to subtitute the suffix (which has to be at the end of the
filename) with ... nothing.
Add to this the fact that "echo" will send its arguments to standard
output and "sed" reads from standard input, makes substitutions and
writes the (possibly modified) line(s) to standard output and an
unmatched substitution is no failure.
This will lead you to the one-line shell script that does it all.
The actual implementation (25 characters, no checks for usage) is left
up to the reader as an exercise.
Hope this helps,
Josef
--
Josef Moellers molle...@sni.de
PS Dieser Artikel enthaelt einzig und allein meine persoenlichen
Ansichten!
PS This article contains my own, personal opinion only!
> > unsuffix report.old .old
> >
> > he will remove the .old extension and the user will be left with a file
> > called "report."
"sed" reads from standard input, makes substitutions and
> writes the (possibly modified) line(s) to standard output and an
> unmatched substitution is no failure.
> The actual implementation (25 characters, no checks for usage) is left
> up to the reader as an exercise.
as long as you're using ksh, here's an even shorter solution:
#!/bin/ksh
mv $1 ${1%${2}}
Cheers,
Douglas Wilson
In ksh I would do this:
echo ${FILE%EXT}
where $FILE is the filename and $EXT is the extension.
- Peter H. Larsen
Thomas Cameron wrote:
> Hi all -
>
> I am taking a shell scripting class, bu the prof fires off info at us
> for about an hour and then leaves campus. We're supposed to be in lab
>
> for two hours, but he's gone and the proctor won't help.
>
> My problem is this: I need to write a ksh script called "unsuffix"
> that
> will take two arguments, the first being a filename and the second
> being
> the extension the user wants to strip from the filename. So if the
> user
> types
>
> unsuffix report.old .old
>
> he will remove the .old extension and the user will be left with a
> file
> called "report."
>
> > unsuffix report.old .old
> > He said we woulf be able to do it with sed, but I don't know how.
>
> Add to this the fact that "echo" will send its arguments to standard
> output and "sed" reads from standard input, makes substitutions and
> writes the (possibly modified) line(s) to standard output and an
> unmatched substitution is no failure.
>
> This will lead you to the one-line shell script that does it all.
I'd love to see that shell script, because I can only think of one where
sed metacharacters in the suffix would wreak havoc.
--
Christian 'naddy' Weisgerber na...@mips.rhein-neckar.de
See another pointless homepage at <URL:http://home.pages.de/~naddy/>.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
for a 1stop sed needs, join seders, the informal sed mailing list.
sed is literate. sed knows 4 R's: read, 'rite, 'rithmetic, recur.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sed informal mailing list
af...@torfree.net 'seders informal mailing list' & moderator
Of half a dozen sed studs known to sedkind, seders has 2 :
Greg Ubben + Carlos J. G. Duarte.
-----
sed web pages (in 4 countries)
http://www.wollery.demon.co.uk seders grab bag (seders official web page
seder Casper
http://www.math.fu-berlin.de/~guckes/sed/ seder, Herr Guckes
http://www.math.fu-berlin.de/~leitner/sed/ seder, Herr von Leitner
http://www.dbnet.ece.ntua.gr/~george/#seders seder, engineer, Dr2b Yiorgos
ftp://olivia.inesc.pt/pub/users/cdua/scripts/sed the great sed stud
ftp://olivia.inesc.pt/pub/users/cdua/scripts/sh seder Carlos J. G. Duarte
----
my favourite DOS/UNIX sed15 (with C source)
(easily compilable for UNIX. has friendly extenstions/relaxations like hex)
ftp://uiarchive.uiuc.edu/pub/systems/pc/simtelnet/msdos/txtutl/sed15.zip
ftp://uiarchive.uiuc.edu/pub/systems/pc/simtelnet/msdos/txtutl/sed15x.zip
Directory: /pub/systems/pc/simtelnet/msdos/txtutl/
Filename Type Length Date Description
===============================================================================
sed15.zip B 62082 910930 Unix-compatible streaming editor v1.5 TC src
sed15x.zip B 20300 910930 Unix-compatible streaming editor v1.5 EXE/docs
sed15.zip has C source, compilable for UNIX.
Also, check simtel for u-sedit. has nice docs.
----
O'Reilly sed books http://www.ora.com
1- sed and awk 2nd edition by Dale Dougherty + Arnold Robbins
(mostly for awk)
2- mastering regular expressions by Jeffrey E. F. Friedl
(mostly for perl, emacs, awk. but touches on sed)
----
newsgroup alt.comp.editors.batch
though seders news group, alt.comp.editors.batch deals with
batch, AI, data warehousing, OLAP, computer security, filters, linguistics,
awk, perl, unix, computer seminars, CFP, etc (even sed)
started by a seder, PhD2b, engineer, the hawk
Yiorgos Adamopoulos.
most newsfeeds do not carry alt.comp.editors.batch
but everyone can get it via dejanews (and others)
you can even follow up, reply to the poster, etc. dejanews is great.
(then he sed: not by sed alone ... lives man)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Douglas Wilson (dgwi...@gte.net) wrote:
: Josef Moellers wrote:
: >
: > > unsuffix report.old .old
: > >
: > > he will remove the .old extension and the user will be left with a file
: > > called "report."
: "sed" reads from standard input, makes substitutions and
: > writes the (possibly modified) line(s) to standard output and an
: > unmatched substitution is no failure.
: > The actual implementation (25 characters, no checks for usage) is left
: > up to the reader as an exercise.
: as long as you're using ksh, here's an even shorter solution:
: #!/bin/ksh
: mv $1 ${1%${2}}
: Cheers,
: Douglas Wilson
--
=-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
al aab, seders moderator sed u soon
it is not zat we do not see the s o l u t i o n
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+
> Thomas Cameron wrote:
> > My problem is this: I need to write a ksh script called "unsuffix" that
> > will take two arguments, the first being a filename and the second being
> > the extension the user wants to strip from the filename. So if the user
> > types
> >
> > unsuffix report.old .old
> >
> > he will remove the .old extension and the user will be left with a file
> > called "report."
> Just think about what you need to do:
> You need to subtitute the suffix (which has to be at the end of the
> filename) with ... nothing.
>
> Add to this the fact that "echo" will send its arguments to standard
> output and "sed" reads from standard input, makes substitutions and
> writes the (possibly modified) line(s) to standard output and an
> unmatched substitution is no failure.
>
> This will lead you to the one-line shell script that does it all.
Yes. Besides sed you can do it with any number of utilities
including (but not limited to) basename, cut, awk and perl,
but the easiest and shortest is way is to use ksh's ${VAR%pattern}
substitution feature (no external commands needed except mv).
> The actual implementation (25 characters, no checks for usage) is left
> up to the reader as an exercise.
Ditto (with even less characters, maybe ~15).
--
Tapani Tarvainen
> > This will lead you to the one-line shell script that does it all.
>
> I'd love to see that shell script, because I can only think of one where
> sed metacharacters in the suffix would wreak havoc.
Yep, you're right. I was only looking at the original poster's example.
Even then, my script would match
fileext .ext
which is incorrect.
I'm sorry.
If you parsed the extension and replaced all metacharacters ... Ah well,
I was wrong.
A better (no, I mean simpler ...) way would be
basename <filename> <extension>
so basename report.old .old
would return
report
this implies, however, that you know the suffix, which I think matches your case
Dom
--
----
Opinions expressed in this mail are mine, and not necessarily that of
Siemens Nixdorf
----
Dominic Lucas - Siemens Nixdorf BU ITS Unix
Tel: +44 1344 850335
Email : Domini...@sni.co.uk
Home: d...@patrol.i-way.co.uk
WWW: http://www.i-way.co.uk/~dom/
----
Very funny Scotty, now beam down my clothes...
----