home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.shell
- Path: sparky!uunet!haven.umd.edu!darwin.sura.net!convex!news.utdallas.edu!hermes.chpc.utexas.edu!aswx266
- From: aswx266@chpc.utexas.edu (Michael Lemke)
- Subject: Re: Substring of a file name
- Message-ID: <1992Sep11.190809.19417@chpc.utexas.edu>
- Organization: The University of Texas System - CHPC
- References: <1992Sep11.090613.8831@schbbs.mot.com>
- Date: Fri, 11 Sep 92 19:08:09 GMT
- Lines: 48
-
- In article <1992Sep11.090613.8831@schbbs.mot.com> W10075@maccvm.corp.mot.com (Stephen Weet) writes:
- >------------------------- Original Article -------------------------
- >From: urs@carp.cis.ufl.edu (Uppili Srinivasan)
- >
- >Hi,
- >
- >I need to write a script to read a substring of some of the filenames
- >
- >in a directory. (i.e) if xy1992 is the name i need to read the last four
- >
- >letters only. I guess 'awk' is ideal for this . But I am curious to
- >
- >know the other ways of doing this.
- >
- >
- >Thanx in advance
- >
- >My e-mail address : urs@carp.cis.ufl.edu
- >------------------------------------------------------------------------
- >
- >If you know the length of the filename in advance you can use the cut
- >command e.g echo filename | cut -c5-8 will give you columns
- >5 to 8.
- >
- > If you do not know the length of the filename or if there is no
- > pattern you can go to awk (Probably the easiest) or try this :-
- >
- >
- > FLENGTH=`echo $filename |wc -l`
- > echo $filename | cut -c`expr $FL - 4 `-`expr $FL - 1 `
- >
- > BEWARE :- Make sure all the spaces are in around expr's operatiors.
-
-
- If you are using expr anyway why not:
-
- expr $filename : '^.*\(....\)'
-
- Gets you the last four characters of filname. The second part is just a
- regular expression. So expr $filename : '\(....\)$' should do as well.
- I didn't test either one which is pretty bad as RE's never do at first
- try what they are supposed to do.
-
- Michael
- --
- Michael Lemke
- Astronomy, UT Austin, Texas
- (michael@io.as.utexas.edu or UTSPAN::UTADNX::IO::MICHAEL [SPAN])
-