The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Opening Large Files

Discussion in 'General Discussion' started by Jeff75, Oct 29, 2003.

  1. Jeff75

    Jeff75 Well-Known Member

    Joined:
    Apr 11, 2003
    Messages:
    555
    Likes Received:
    0
    Trophy Points:
    16
    I need to view part of a large file (over 100 MBs) and was wondering if there's anyway that I can just view the first 50 lines or so at the very top or very bottom of the file. If so, can somebody let me know what command to use?

    Also, if I need to edit the file, is there anyway to do this by only editing the first or last few lines of the file? If I try to open the entire thing, it takes forever and never ends up letting me edit the file.
     
  2. christi1

    christi1 Well-Known Member

    Joined:
    Oct 20, 2003
    Messages:
    65
    Likes Received:
    0
    Trophy Points:
    6
    Location:
    Texas, USA
    Try this:

    Creat a file named viewer.php and copy/paste the following into it:

    <?php
    // number of lines to show
    $lines = "50";

    // define our starting count
    $count = "0";

    // open the file
    $fp = fopen ("/path/to/your/file.txt", "r");
    $buffer = fread($fp, 100000);
    fclose ($fp);
    $stuff = explode("\n", "$buffer");

    if(is_array($stuff)) {
    foreach($stuff as $line) {
    // we don't want to count empty lines, so...
    if($line != "") {
    $count++;
    // if the line number is less than the number of lines you stated above, print it
    if($count < $lines) {
    echo "$line<br>\n";
    }
    }
    }
    }
    ?>

    That will open your file (provided you have access to do so) and print the first 50 lines for you. Can change the 50 to however many lines you want to see.
     
    #2 christi1, Oct 29, 2003
    Last edited: Oct 29, 2003
  3. ciphervendor

    ciphervendor Well-Known Member

    Joined:
    Aug 26, 2002
    Messages:
    1,052
    Likes Received:
    0
    Trophy Points:
    36
    vi/vim is super efficient as well. Whatever you do, do not use pico ;)
     
Loading...

Share This Page