Wednesday, 15 April 2015

regex - read a line from file and if pattern match then delete it from it in perl -


I have a file with content place.txt: say:

  Go to Rome Will go I do not want to go to Rome, I will not go. I do not want to go to Rome, I will not go. I'll go to Rome. I would not want to go to India. I would not want to go to India. I'll go to Rome.  

I want to read this file and want to match the lines with the pattern "I want to go to Rome." And leave those lines that match the pattern in this file in Pearl.

My sample code is:

  $ file = new IO :: file; $ File- & gt; Open ("& lt; jobs.txt") or dead "jobs.txt can not be opened"; While (my $ line = $ file- & gt; gets) {next if $ line = ~ m {/ I want to go to Rome}; Print $ line; } Close $ file;  

Note: Will my file be a big one, can we use SAD or Anch?

this is as simple as

  until perl -ine'print / I do not want to go to Rome / ' 

If you prefer scripts

  use strict; Use warnings; Use Autodi; Use static FILENAME = & gt; 'Jobs.txt'; Open my IN $, '& lt;', FILENAME; While {($. == 1) {if you need to do this after reading the first line, open $ $ i-node with my original file, & Gt; file name; Select $ Out; } Until print / I do not want to go to Rome; }  

No comments:

Post a Comment