Operating System - HP-UX
1830226 Members
1613 Online
109999 Solutions
New Discussion

parsing sed command using a variable that has a special character in it

 
Dave Cassel
Occasional Contributor

parsing sed command using a variable that has a special character in it

Can this be done in ksh or PERL or will I have to use C? I have a variable called save_passwd that can contain any sized encrypted passwd with special characters in it. If my orig_passwd is changed from save_passwd I want to replace save_passwd with orig_passwd. But I can't use a simple sed command because of the special character in the encrypted passwd. The sed command will not be parsed when the variables have these values in them. See my sample:

save_passwd=4/ZZcVJkiflO3w
orig_passwd=t98zVK47301.27

eval sed -e 's/${user}:"${save_passwd}":${save_dt}/${user}:"${orig_passwd}":${exp_dt}/' /passwd_work/copy_passwd > /passwd_work/copy_passwd.tmp

3 REPLIES 3
Hoefnix
Honored Contributor

Re: parsing sed command using a variable that has a special character in it

I don't now if the / is the only special character, but you can replace this in the sed command for example with |(pipe)
sed 's|abc|def|'

I hope this helps.

Regards,

Peter
H.Merijn Brand (procura
Honored Contributor

Re: parsing sed command using a variable that has a special character in it

assuming that the variable is set in the shell's environment, one can access it in a parl statement through the %ENV hash. Above example would roughly translate to

perl -pe's/$ENV{user}:$ENV{save_passwd}:$ENV{save_dt}/$ENV{user}:$ENV{orig_passwd}:$ENV{exp_dt}/' /passwd_work/copy_passwd > /passwd_work/copy_passwd.tmp

perl can also do in-line edit. see 'man perlrun' under the option -i

Above example looks like the commands in the context leading towards this question could be done in perl too

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Dave Cassel
Occasional Contributor

Re: parsing sed command using a variable that has a special character in it

Thanks so much! I didn't realize this reg expression can be delimited by any character except newline. I also just found your answer in the O'Reilly Sed & Awk book pg. 81!