I have a file


I am reading each in value in a variable, now I need to replace the 2nd variable with a particular string. like

$1st = "/abc"   $replace1 = "/abc/pqr"
$2nd = "abc"    $replace2 = "abc1"
$3rd = "xyzabc" $replace3 = "mnpabc"

I used perl -0777 -pi -2 's%$1st%$replace1%' file this does 1st line replacement

I used perl -0777 -pi -e 's/3rd/$replace1/ file this does the 3rd line.

but the moment I replace $2nd with $replace , it replaces all places where ever there is "abc". how can I restrict this to replace only one element at a time?

  • Anyone having some idea on this – Guestme Feb 22 '13 at 20:28

Hard to understand exactly what you're doing but your input file fits the format of a configuration file (like windows ini or unix conf). I would suggest that you use a Perl module which can help read and write this format easily. Try Config::Simple or Config::INI from CPAN.

Otherwise, are you using "perl -0777" (ignore record separators) intentionally? This is unusual to me as is this list of one-liners in general.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.