Replacing single quotes and backslashes in strings

P

Phil Rhoades

People,

I have been trying to find a solution to this on the net but with no luck:

I want to get a list of file names from a dir and pass the result to a du command eg:

puts `du file_name`

but if the filename has single quotes or backslashes in it eg:

puts `du phil'sfile_name` or: puts `du phil\sfile_name`

I have replace them with eg:

puts `du phil\'sfile_name` or: puts `du phil\\sfile_name`

I have tried all sorts of combinations with gsub with no success . .

Is there a solution with gsub or something else?

Thanks,

Phil.
--
Philip Rhoades

Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275)
GPO Box 3411
Sydney NSW 2001
Australia
Mobile: +61:(0)411-185-652
Fax: +61:(0)2-8221-9599
E-mail: (e-mail address removed)
 
L

Logan Capaldo

People,

I have been trying to find a solution to this on the net but with
no luck:

I want to get a list of file names from a dir and pass the result
to a du command eg:

puts `du file_name`

but if the filename has single quotes or backslashes in it eg:

puts `du phil'sfile_name` or: puts `du phil\sfile_name`

I have replace them with eg:

puts `du phil\'sfile_name` or: puts `du phil\\sfile_name`

I have tried all sorts of combinations with gsub with no success . .

Is there a solution with gsub or something else?

Thanks,

Phil.
--
Philip Rhoades

Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275)
GPO Box 3411
Sydney NSW 2001
Australia
Mobile: +61:(0)411-185-652
Fax: +61:(0)2-8221-9599
E-mail: (e-mail address removed)

str = %q{du phil'sfile_name}
puts `#{str.gsub(/['\\]/) { |meta_char| case meta_char; when "\\"; "\\
\\"; when "'"; "\\'"; end }}`
 
P

Philip Rhoades

Logan,

People,

I have been trying to find a solution to this on the net but with
no luck:

I want to get a list of file names from a dir and pass the result
to a du command eg:

puts `du file_name`

but if the filename has single quotes or backslashes in it eg:

puts `du phil'sfile_name` or: puts `du phil\sfile_name`

I have replace them with eg:

puts `du phil\'sfile_name` or: puts `du phil\\sfile_name`

I have tried all sorts of combinations with gsub with no success . .

Is there a solution with gsub or something else?

Thanks,

Phil.
--
Philip Rhoades

Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275)
GPO Box 3411
Sydney NSW 2001
Australia
Mobile: +61:(0)411-185-652
Fax: +61:(0)2-8221-9599
E-mail: (e-mail address removed)


str = %q{du phil'sfile_name}
puts `#{str.gsub(/['\\]/) { |meta_char| case meta_char; when "\\"; "\\
\\"; when "'"; "\\'"; end }}`


Whew!

Why doesn't this work?:


line = line.gsub( /'/ ', "\'" )
line = line.gsub( '\\', "\\\\" )
puts `du #{line}`


Thanks,

Phil.
--
Philip Rhoades

Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275)
GPO Box 3411
Sydney NSW 2001
Australia
Mobile: +61:(0)411-185-652
Fax: +61:(0)2-8221-9599
E-mail: (e-mail address removed)
 
L

Logan Capaldo

Logan,

People,

I have been trying to find a solution to this on the net but with
no luck:

I want to get a list of file names from a dir and pass the result
to a du command eg:

puts `du file_name`

but if the filename has single quotes or backslashes in it eg:

puts `du phil'sfile_name` or: puts `du phil\sfile_name`

I have replace them with eg:

puts `du phil\'sfile_name` or: puts `du phil\\sfile_name`

I have tried all sorts of combinations with gsub with no success . .

Is there a solution with gsub or something else?

Thanks,

Phil.
--
Philip Rhoades

Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275)
GPO Box 3411
Sydney NSW 2001
Australia
Mobile: +61:(0)411-185-652
Fax: +61:(0)2-8221-9599
E-mail: (e-mail address removed)


str = %q{du phil'sfile_name}
puts `#{str.gsub(/['\\]/) { |meta_char| case meta_char; when "\\";
"\\
\\"; when "'"; "\\'"; end }}`


Whew!

Why doesn't this work?:


line = line.gsub( /'/ ', "\'" )
line = line.gsub( '\\', "\\\\" )
puts `du #{line}`


Thanks,

Phil.
--
Philip Rhoades

Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275)
GPO Box 3411
Sydney NSW 2001
Australia
Mobile: +61:(0)411-185-652
Fax: +61:(0)2-8221-9599
E-mail: (e-mail address removed)

Well, since `` invokes your shell you have to sort of "double-escape"
the meta characters of single quotes. For instance with single quotes
if you escape it once just for ruby, its not escaped for the shell so
it may try to quote with single quotes instead of considering the
single quote part of the file name. line.gsub('\\', "\\\\") should
work but you have to be a little more careful with quotes. eg.
line.gsub(/'/, "\\'"). My code was meant to be extra-smart about the
escaping. You don't want to "over-escape" either.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top