From: Philip Rhoades Date: 2005-12-20T19:41:35+09:00 Subject: Re: Replacing single quotes and backslashes in strings Logan, On Tue, 2005-12-20 at 19:02 +0900, Logan Capaldo wrote: > On Dec 20, 2005, at 4:15 AM, Phil Rhoades wrote: > > > 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: phil@pricom.com.au > > > 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: phil@pricom.com.au