Re: How To Insert an Escaped Apostrophe With Sed?

On Wed, 1 Feb 2012 08:16:26 -0800 (PST), Rick <rickreineman@xxxxxxxxx>

The problem to solve are UNIX filesystems full of user files with
special characters in the names. These are the typical characters )
(*', etc.

The approach I've taken is to use the find command to produce an input
list of filenames. Then manipulate those entries and produce a script
full of move commands to rename the offending files. The script full
of move commands ends up with the special character escaped in the
source field and the destination field with no special character at

For example, an original file named:

is renamed as follows:
/usr/bin/mv /home/rick/file\(name\).txt /home/rick/filename.txt

Here is an example of the input list creation and processing, we'll
look for left parens and remove them altogether:
find . -type f -name \*\(\* >> list

awk '{ print $1, $1 }' list | while read a b; do
echo $b | sed -e 's/(//g' | read c
echo $a | sed -e 's/(/\\\\(/g' | read d
echo /usr/bin/mv $d $c >> fix-it

The fix-it script ends up with a bunch of move entries all escaped and
ready to execute.

I have all the special character substitutions solved except the
apostrophe. I can get sed to remove the apostrophe from an input
line. What I cannot figure out is how to get the escaped apostrophe
to echo into the script of move commands. I have messed with every
conceivable combination of escapes and quotes, any I can conceive

I want to end up with a move command something like:
/usr/bin/mv /home/rick/filename\'s.txt /home/rick/filenames.txt

I can not get the \' to work.

For the specific problem:

echo "fo'o'bar" | sed 's/'\''/\\&/g'


echo "fo'o'bar" | sed 's/'\''/\\'\''/g'


echo "fo'o'bar" | sed "s/'/\\\\&/g"


echo "fo'o'bar" | sed "s/'/\\\\'/g"

plus probably others.

for the general issue of mass renaming, see