0

I have a file which contains

...

CNN
111
XXX

ABC
111
XXX

ABC
111
BBC

...

and I need to change the 111 to 999 but only as part of ABC\n111\nXXX

...

CNN
111
XXX

ABC
999
XXX

ABC
111
BBC

...

I have tried this, but it changes "111" everywhere.

perl -i -pe '/ABC\n111\nXXX/ if s/111/999/g' FILE

Note: We need to compare multiple lines as "111" might be in many other places. The file size is 227kb.

3
  • 2
    All-caps text and nondescript titles are not OK. – phk Jan 1 '17 at 23:59
  • How big is the file roughly? There are essentially two approaches, the easy one where you read the entire file into memory and the harder one where you process it a few lines at a time. – icarus Jan 2 '17 at 0:07
  • @Farhana Please login back on again before trying to edit your post. – phk Jan 2 '17 at 1:25
1
perl -0777 -i -p -e 's/ABC\n111\nXXX/ABC\n999\nXXX/g;' file

the -0777 puts perl into slurp mode so it reads the whole file. Then the substitute command makes the needed change.

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.