This article will use the php and linux sort Command two methods, respectively, to implement the file content de-RE and sort, and provide the completion of the demo code.
1. Create a test file
Write 1 million numbers, one number per line
<?php$file = ' User_id.txt '; $num = 1000000; $tmp = "; for ($i =0; $i < $num; $i + +) { $tmp. = Mt_rand (0,999999). Php_eol; if ($i >0 && $i%1000==0 | | $i = = $num-1) { file_put_contents ($file, $tmp, file_append); $tmp = '; }}? >
View the number of file lines
Wc-l user_id.txt 1000000 User_id.txt
2.php for de-weight and sequencing
Because you want to process 1000000 rows of data, you set PHP to use 256mof memory to prevent running out of memory.
<?php/** * File Content de-order * @param string $source source file * @param string $dest destination file * @param string $order sort order * @ Param Int $sort _flag sort type */function fileunisort ($source, $dest, $order = ' asc ', $sort _flag=sort_numeric) {//Read file contents $file _data = file_get_contents ($source); The contents of the file are split by row into arrays $file _data_arr = Explode (Php_eol, $file _data); Remove empty row data $file _data_arr = Array_filter ($file _data_arr, ' filter '); Go to heavy $file _data_arr = Array_flip ($file _data_arr); $file _data_arr = Array_flip ($file _data_arr); Sort if ($order = = ' asc ') {sort ($file _data_arr, $sort _flag); }else{Rsort ($file _data_arr, $sort _flag); }//number combination to file content $file _data = implode (Php_eol, $file _data_arr). Php_eol; Write file File_put_contents ($dest, $file _data, True);} Filter Empty line function filter ($data) {if (! $data && $data!== ' 0 ') {return false; } return true; Settings can use memory for 256mini_set (' Memory_limit ', ' 256m '); $source = ' user_id.txt '; $dest = ' Php_sort_user_iD.txt '; Fileunisort ($source, $dest);? >
To view the files that have been re-sorted
Wc-l php_sort_user_id.txt 632042 php_sort_user_id.txthead php_sort_user_id.txt 012357891112 ...
3.linux Sort command for de-weight and sorting
The Linux Sort command is used to sort text files by row
Format:
Sort [OPTION] ... [FILE] ...
Parameter description:
- u Go heavy
- n Numeric sort type
- R Descending
- o path to output file
Use sort to perform de-weight and sort
Sort-uno Linux_sort_user_id.txt User_id.txt
To view the files that have been re-sorted
Wc-l linux_sort_user_id.txt 632042 linux_sort_user_id.txthead linux_sort_user_id.txt 012357891112 ...
Summary: The use of PHP or Linux Sort command can achieve file de-weight and sorting, execution time difference is not small, but it is recommended for the operation of the file class, directly using the system command implementation is simpler.
This article will use the php and linux sort Command two methods, respectively, to implement the file content de-RE and sort, and provide the completion of the demo code.
This article explains the contents of the document to re-emphasis and sorting related content, more information please focus on the PHP Chinese web.