Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

read a big text file and copy the selected lines to another file

Tags:

file

text

php

fopen

I need to create a database table from here:

So, I copied the same file to a server folder using curl command. I need to retrieve 00-00-00 (hex) XEROX CORPORATION, 00-00-01 (hex) XEROX CORPORATION etc. from the above text file. I need to copy only the hex values and the first line of organisation to another text file.

How can I do it with PHP?

like image 643
NewPHP Avatar asked Jan 26 '26 13:01

NewPHP


2 Answers

  <?php
  // open the input and the output
  $fp = fopen("http://standards.ieee.org/develop/regauth/oui/oui.txt","r");
  $out = fopen("somefile.txt","w");

  while($rec = fgets($fp)){
      // check to see if the line contains '(hex)'
      if (strpos($rec, '(hex)') !== false){
          // if so write it out
          fputs($out, $rec."\n");              
      }
  }
  fclose($fp); 
  fclose($out);
like image 171
Orangepill Avatar answered Jan 28 '26 01:01

Orangepill


Try this one

<?php
        $i          = 1;
        $a_line     = "";
        $first_line = "";
        $handle     = @fopen("/tmp/inputfile.txt", "r");

        if ($handle) {
            while (($buffer = fgets($handle, 4096)) !== false) {

                if($i == 1)
                    // here you have first line
                    $first_line = $buffer;
                else {

                    // check the $buffer contains the substring (hex) and XEROX CORPORATION
                    // if it contains put it in another variable

                    $a_line .= $buffer;

                }



            }
            if (!feof($handle)) {
                echo "Error: unexpected fgets() fail\n";
            }
            fclose($handle);
        }
        // finally write the $first_line to a header file
        // then write $a_line to another file

    ?>
like image 25
Kiren S Avatar answered Jan 28 '26 03:01

Kiren S



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!