home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / unix / question / 10207 < prev    next >
Encoding:
Internet Message Format  |  1992-08-19  |  1.6 KB

  1. Path: sparky!uunet!usc!wupost!waikato.ac.nz!aukuni.ac.nz!ecmtwhk
  2. Newsgroups: comp.unix.questions
  3. Subject: Re: Changing file names
  4. Message-ID: <1992Aug20.104721.28955@ccu1.aukuni.ac.nz>
  5. From: ecmtwhk@ccu1.aukuni.ac.nz (Thomas Koenig)
  6. Date: Thu, 20 Aug 1992 10:47:21 GMT
  7. References: <ac#n06l.bosak@netcom.com> <ceYpFNG00VoxBKON4O@andrew.cmu.edu>
  8. Organization: University of Auckland, New Zealand.
  9. Lines: 43
  10.  
  11. jf41+@andrew.cmu.edu (Jonathan R. Ferro) writes:
  12.  
  13. >bosak@netcom.com (Jon Bosak) writes:
  14. >> [ file name conversion: xyz920815a ==> 920815a ]
  15. >>
  16. >> Who can provide the most elegant solution using just the basic Unix
  17. >> tools (cp, mv, sed, awk, sh, csh)?
  18.  
  19. [perl script deleted]
  20.  
  21. >Unless, of course, you don't consider perl to be a "basic" Unix tool, in
  22. >which case you're hopelessly behind the times.  :-)
  23.  
  24. On the day when all the systems I use run perl, I'll be happy to use
  25. that script, but I lack the disk space and the time to install perl
  26. myself. And no, I get to see the new day earlier than most people at the
  27. moment :-)
  28.  
  29. Until then, I will use my own home - cooked script, which follows. It
  30. will break on file names containing newlines and whitespace, but my
  31. filenames don't contain these :-).
  32.  
  33. #! /bin/sh
  34. if test $# -eq 0 -o $# -eq 1 -o $# -eq 2
  35. then
  36.     echo "usage: $0 regexp newexp files" 
  37.     exit 1
  38. fi
  39. re=$1
  40. ne=$2
  41. shift 2
  42. ls $* |
  43. nawk \
  44. '$0 ~ re { var=$0
  45.     sub(re,ne,var)
  46.     print "ls",var " >/dev/null 2>&1 && echo",var,
  47.     "not clobbered 1>&2 || mv",
  48.     $0,var
  49.     } ' re=$re ne=$ne 
  50. -- 
  51. Thomas Koenig, ecmtwhk@ccu1.aukuni.ac.nz, ib09@rz.uni-karlsruhe.de
  52. The joy of engineering is to find a straight line on a double logarithmic
  53. diagram.
  54.