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, 2017 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, 2017 at 0:07
  • @Farhana Please login back on again before trying to edit your post.
    – phk
    Jan 2, 2017 at 1:25

1 Answer 1

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.

You must log in to answer this question.

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