Re: How To Insert an Escaped Apostrophe With Sed?



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

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
all.

For example, an original file named:
/home/rick/file(name).txt

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
done

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
anyway.

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'

or

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

or

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

or

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

plus probably others.

for the general issue of mass renaming, see

http://mywiki.wooledge.org/BashFAQ/030

.



Relevant Pages

  • How To Insert an Escaped Apostrophe With Sed?
    ... Then manipulate those entries and produce a script ... full of move commands to rename the offending files. ... source field and the destination field with no special character at ... conceivable combination of escapes and quotes, ...
    (comp.unix.shell)
  • Re: How To Insert an Escaped Apostrophe With Sed?
    ... These are the typical characters) ... Then manipulate those entries and produce a script ... full of move commands to rename the offending files. ... source field and the destination field with no special character at ...
    (comp.unix.shell)
  • =?ISO-8859-1?Q?New_System_Command_Script_Facility?=
    ... I'm currently finished writing a new replacement command script processor ... (at least that RACF thinks so, and better support for non-RACF sites). ... The IF-type commands now have ELSE processing ... non-scroll highlighted console message and BEEP until a specific JOB or TASK ...
    (bit.listserv.ibm-main)
  • Re: Command Line Interface
    ... >> should accept system commands only for an administrator. ... >> how should I supress the bash shell and launch my custom CLI ... > The script could run in a restricted shell and offer a menu of ... account, and either 1) change account password, 2) ...
    (comp.os.linux.misc)
  • Net::Telnet and SMTP
    ... I'm trying to send a sequence of commands and then log both the ... I've included the script and a sample text file (that feeds the scripts ... sub processfile { ... #Try a short delay between sending command and reading output ...
    (comp.lang.perl.misc)