home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / unix / shell / 3909 < prev    next >
Encoding:
Text File  |  1992-09-11  |  1.9 KB  |  59 lines

  1. Newsgroups: comp.unix.shell
  2. Path: sparky!uunet!haven.umd.edu!darwin.sura.net!convex!news.utdallas.edu!hermes.chpc.utexas.edu!aswx266
  3. From: aswx266@chpc.utexas.edu (Michael Lemke)
  4. Subject: Re: Substring of a file name
  5. Message-ID: <1992Sep11.190809.19417@chpc.utexas.edu>
  6. Organization: The University of Texas System - CHPC
  7. References: <1992Sep11.090613.8831@schbbs.mot.com>
  8. Date: Fri, 11 Sep 92 19:08:09 GMT
  9. Lines: 48
  10.  
  11. In article <1992Sep11.090613.8831@schbbs.mot.com> W10075@maccvm.corp.mot.com (Stephen Weet) writes:
  12. >------------------------- Original Article -------------------------
  13. >From: urs@carp.cis.ufl.edu (Uppili Srinivasan)
  14. >
  15. >Hi,
  16. >
  17. >I need to write a script to read a substring of some of the filenames
  18. >
  19. >in a directory. (i.e) if xy1992 is the name i need to read the last four
  20. >
  21. >letters only. I guess 'awk' is ideal for this . But I am curious to
  22. >
  23. >know the other ways of doing this.
  24. >
  25. >
  26. >Thanx in advance
  27. >
  28. >My e-mail address : urs@carp.cis.ufl.edu
  29. >------------------------------------------------------------------------
  30. >
  31. >If you know the length of the filename in advance you can use the cut
  32. >command e.g     echo filename | cut -c5-8  will give you columns
  33. >5 to 8.
  34. >
  35. >   If you do not know the length of the filename or if there is no
  36. >   pattern you can go to awk (Probably the easiest) or try this :-
  37. >
  38. >
  39. >        FLENGTH=`echo $filename |wc -l`
  40. >        echo $filename | cut -c`expr $FL - 4 `-`expr $FL - 1 `
  41. >
  42. >   BEWARE :- Make sure all the spaces are in around expr's operatiors.
  43.  
  44.  
  45. If  you are using expr anyway why not:
  46.  
  47. expr $filename : '^.*\(....\)'
  48.  
  49. Gets you the last four characters of filname.  The second part is just a
  50. regular expression.  So expr $filename : '\(....\)$' should do as well. 
  51. I didn't test either one which is pretty bad as RE's never do at first 
  52. try what they are supposed to do.
  53.  
  54. Michael
  55. -- 
  56. Michael Lemke
  57. Astronomy, UT Austin, Texas
  58. (michael@io.as.utexas.edu or UTSPAN::UTADNX::IO::MICHAEL [SPAN])
  59.