Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6576735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:32:58+00:00 2026-05-25T15:32:58+00:00

Is it possible to delete all lines that have the same first 30 characters

  • 0

Is it possible to delete all lines that have the same first 30 characters of the line and then only to left the first line that has these characters?

Example:

xx2 Lorem ipsum dolor sit amet, fdsfdsfs
xx2 Lorem ipsum dolor sit amet, 43434343

The second should be deleted… Hope it’s possible… Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:32:59+00:00Added an answer on May 25, 2026 at 3:32 pm

    If you need to deal with really large files, reading only one line at a time and writing to a temp file will consume less memory. Using a temp file and renaming it to the input file when done will do the operation atomically without losing your original file. Checking for array keys instead of values will offer fast lookup since keys are indexed. You also need to handle the edge case of a blank line returning false on substr.

    <?php
    $infile_name = "infile.txt";
    
    $seen = array();
    $infile = fopen($infile_name, "r");
    if ( $infile !== false ) {
        // Temporary file to write results to
        $outfile_name = tempnam(sys_get_temp_dir(), 'tmp');
        $outfile = fopen($outfile_name, "w");
    
        while (!feof($infile)) {
            $line = fgets($infile);
            if ( $line == '' ) {
                // blank line, just write it
                fwrite($outfile, $line);
            }
            else {
                $prefix = substr( $line, 0, 30 );
    
                if ( !array_key_exists($prefix, $seen) ) {
                   fwrite($outfile, $line);
    
                   // Store the prefix as a key for fast indexed lookup
                   $seen[$prefix] = true;
                }
            }
        }
    
        fclose($infile);
        fclose($outfile);
    
        // Remove the old file and put the new file in its place
        unlink($infile_name);
        rename($outfile_name, $infile_name);
    }
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Supposing I have a File f that represents a directory, then f.delete() will only
Is it possible to delete multiple elements from a list at the same time?
is there anyway to delete every character on the same line as a cursor,
Scenario: I have a text file that has pipe (as in the | character)
I have a table that has an identity column as the Primary Key. None
I have a Model1.edmx, which has several tables. Now, I do not want all
I have a table that contains id username password last_touch It is possible to
I have an App that was working perfectly, then I released an update and
I need a solution to delete duplicate lines where first field is an IPv4
Is it possible to get a list of all files modified/added/deleted by a particular

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.