Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
    
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.

share|improve this answer

You must log in to answer this question.

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