Walt Mankowski on 21 Aug 2009 12:34:05 -0700


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] the character least likely to find in a Unix file name


On Fri, Aug 21, 2009 at 03:09:08PM -0400, TuskenTower wrote:
> We have an interesting point of discussion about the following two sed lines:
>    PATH=`echo $PATH | sed "s"'$'"$old_paht"'$'"$new_path"'$'`
> VS
>    PATH=`echo $PATH | sed "s,$old_path,$new_path,"`
> 
> We would like to have a sed script that modifies the PATH in a way
> that follows the principle of least astonishment.
> 
> Someone decided the dollar sign was the least likely to find in a
> filename and produced the above code.  Another voiced concern over
> legibility and to stick to characters that don't require escaping.
> Another said the semi-colon was the best option.  The discussion
> degenerated into using tilde, back tick and a few others that cause
> weird things.
> 
> Any thoughts?

Rather than guessing what characters might occur or not occur in the
paths, you could replace the sed script with a simple perl one-liner
than takes the two paths as input parameters:

  PATH=`perl -e '$_ = <STDIN>; s/$ARGV[0]/$ARGV[1]/; print' old_path new_path`

For example, if you wanted to change local to LOCAL you could run

  PATH=`perl -e '$_ = <STDIN>; s/$ARGV[0]/$ARGV[1]/; print' local LOCAL`

Note that that only changes the first occurence; if you want to make a
global change to all the occurences, change the program to

  PATH=`perl -e '$_ = <STDIN>; s/$ARGV[0]/$ARGV[1]/g; print' local LOCAL`

I should note that I'm not at all a sed expert, so I may have
misunderstood your problem.  If I did, my apologies.

Walt

Attachment: signature.asc
Description: Digital signature

___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug