Details of the contents of the content to be re-sorted and related content

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.