Delete lines with a specific pattern at the beginning of them

I have a text file containing approximately 25,000 lines. About 525kb.

Some lines have random text at the beginning. Some of them have long lines with a comma.

Some others have only three semi-colonies, followed by a space and optionally text on the same line. These are the lines that I want to delete.

Here is an example ....

;;; Updated Time 20120706122706 ;;; Generic DEveloper Output ;;; Some Random Comments ;;; I got some more... ;;; Yet another uneeded line ;;; Thanks for using StackOverflow <http://stackoverflow.com>, or... ;;; Not. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Banana Production [Data_Release_Version] Version=12586 Released=20120706122706 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Baseline Properties [BaseLineProperties] Comment=BaselineProperties 

----- etc.

As soon as it hits the first line with 4 or more; in the line I need the rest of the file, since there are no lines ";;;".

Trying to find something quickly, instead of reading the entire line and writing it back if it does not match ";;;".

A file is an ASCII text file (possibly UTF-8).

Any ideas?

Thank you for your time, help and knowledge.

+4
source share
3 answers

I would suggest using file_get_contents() and save the contents of the file in a variable as a string, and then use explode() this line for each newline character, and then use preg_match() in the foreach to check if the line starts with 3 semicolons and space, if it is docent, put it in another array called $output . After foreach implode() $output and add a newline and use file_put_contents() to print in another file. Hope this helps :-)

code:

 <?php $string = file_get_contents($filename); $array = explode("\n",$string); foreach($array as $arr) { if(!(preg_match("^;;;\s",$arr))) { $output[] = $arr; } } $out = implode("\n",$output); file_put_contents($path,$out); ?> 
+4
source

Depends ... I would try to load into a string, and then do explode () with a new string, so in an array, and then run foreach with a pass on any one that doesn't have strpos == 0 -AND-strpos! == false, you can put a continuation to go to the next line if it does not match.

Another option is to parse, skip, or even use fseek, etc. Depends on many different factors to determine what will be the fastest.

You can explode later and add new lines, and then pop the file and / or use line breaks. Depending on where the exit should go.

+1
source

I think you yourself answered:

Make a script that reads the input file line by line in a (while) loop. It writes each line to the output file if two conditions are met: 1. the flag ("done") is FALSE and 2. the line does NOT start with ";;;" (not empty). This removes these lines starting at three semicolons. When you exit a line with more semicolons, you set the flag to TRUE, so the remaining lines will be copied without verification.

0
source

All Articles