0

I have a file

love=/abc
day=abc
night=xyz
afternoon=xyzabc

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?

1
  • Anyone having some idea on this
    – Guestme
    Feb 22, 2013 at 20:28

1 Answer 1

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .