Sign up ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
Anyone having some idea on this – Guestme Feb 22 '13 at 20:28

1 Answer 1

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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