perl or sed s/// problem

Eric Shubert ejs at shubes.net
Thu Aug 5 11:08:42 MST 2010


David wrote:
> I'm having a problem with perl *and* sed with a particular replace line.  The lines I'm trying to search for, and replace, both have "/" characters, which is completely killing me.   
> 
> line="password    requisite     /lib/security/$ISA/pam_cracklib.so retry=3"
> newline="password required /lib/security/$ISA/pam_cracklib.so minlen=8 dcredit=-1 ucredit=-1 lcredit=0 minclass=3  retry=3"
> 
> I need to maintain whitespace, too.
> 
> perl -p -i -e s/"$line"/"$newline"/  file
> 
> That tosses out all kinds of errors, since perl believes the first "/lib" line to be the next delimiter.  Even using the magical \Q and \E things doesn't work.  AARRGGHH!!!   
> 
> Sed seems to have the same problem.   Anyone have any ideas on how I can get this to work?  And yes, unfortunately, I need to use the variables.
> 
> Thanks,
> David
> 
> 

.) you should probably have quotes around the -e argument, as in:
perl -p -i -e "s/$line/$newline/" file

.) I'm not positive, but I believe that the character immediately after 
the s becomes the separator, so you could use s|from|to| or s^from^to^ 
or whatever. Otherwise, simply use \ before the special character to 
escape it from being interpreted as being special (iow, treat it literally).

-- 
-Eric 'shubes'



More information about the PLUG-discuss mailing list