[CLUE-Tech] sed help

clue hp205ctl at hotpop.com
Wed Jan 14 17:34:01 MST 2004


David Willson wrote:
> I'm going crazy here.
> 
> I want to take strings returned by 'find' and replace the folder-name I was
> searching in with another folder-name, but sed is ~not~ behaving as I want
> it to.
> This works at the command-line:
> 
>>echo "/home/david/.kde" | sed -e s/"\/home\/david"/"\/backups\/david"/
> 
> /backups/david/.kde
> 
> Now, how do I make it work in a script?
> 
> Ultimately, I want something like this:
> find /home/david -type d -exec [! -d `sed -e
> s/'/home/david'/'/home/backups/david'/` ] echo "Error: Directory no found!"


try simplifying -- use simpler commands and then pipe them together :

  find /home/david -type d  |  sed -e 's/\/home\/david/\/home\/backup\/david\'

also, to make it more readable and less error prone, change the sed separator:  (gnuism*)

  find /home/david -type d  |  sed -e 's|/home/david|/home/david/backups|'


or make a loop to do something with it:


  for i in `find /home/david -type d`
  do
  n=`ech $i | sed -e 's|/home/david|/home/david/backups|'`
  echo "do something with $i and $n"
  done


note that for large numbers of directories this will be less efficient, because it will invoke "sed" for each instead of just once for the lot.

Todd

* - does not work on most "legacy" unix's, unless they have nice up-to-date gnu tools like linux...




More information about the clue-tech mailing list