#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2006
    Posts
    34
    Rep Power
    8

    Remove empty lines ?


    I have this lines:

    Code:
    Text line1
    
    
    Text line2
    
    
    
    
    
    Text line3
    Between "text line1" and "text line2" exist 2 blank line. Between "Text line2" and "text line3" exist 5 blank lines.

    Is possible to specify using a regexp pattern the following : ?

    If more than 1 blank line exist in document remove all other blank line, and leave only one. Something like:


    Code:
    Text line1
    
    Text line2
    
    Text line3
    Thanks!
  2. #2
  3. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,897
    Rep Power
    3886
    Sure it's possible. However, Dev Shed is not a free program-writing service so you're unlikely to have somebody just give it to you.

    Why don't you go through how you're thinking of approaching it for us and we can assist from there?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2006
    Posts
    34
    Rep Power
    8
    I need a pattern and not a program!
  6. #4
  7. Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Sep 2007
    Location
    outside Washington DC
    Posts
    2,642
    Rep Power
    3699
    ezy,

    you need to adjust your attitude if you want to get help.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jul 2004
    Location
    Middle Europa
    Posts
    1,200
    Rep Power
    14
    on *nix try
    man sed
    working on Solaris[5-9], preferred languages french and C.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2006
    Posts
    34
    Rep Power
    8
    If you see, I don't ask for a complete application. This solution work great:

    Code:
    preg_replace("!^\s+(\D)!m", "\\1", $text);
    Now I have something like this:

    Code:
    1. Question1
    A: answer1
    B: answer1
    C: answer1
    2. Question2
    A: answer2
    B: answer2
    C: answer2
    I don't understand how to check if a blank line exist before any number (for example: 1 or 2). If blank line not exist put a blank line. Finnaly I need this output:

    Code:
    1. Question1
    A: answer1
    B: answer1
    C: answer1
    
    2. Question2
    A: answer2
    B: answer2
    C: answer2
    I read the whole preg_match documentation but I don't understand how to solve this issue.
  12. #7
  13. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,547
    Rep Power
    2337
    This may be a case where carrying the full load via regex is difficult and unnecessary. This seems to work:

    php Code:
    $fp_in = fopen("input.txt", "r");
    $fp_out = fopen("output.txt", "w");
     
    $previous_line = FALSE;
    $current_line = "";
     
    // Read file line-by-line
    while (FALSE !== ($current_line = fgets($fp_in)))
    {
        // Check for leading digit
        if (0 != preg_match("/^\d.*/", $current_line))
        {
            // Check if previous_line was not blank
            if (FALSE !== $previous_line &&
                "\n" != $previous_line[0])
            {
                // Need to add a blank line
                fputs($fp_out, "\n");
            }
        }
     
        fputs($fp_out, $current_line);
        $previous_line = $current_line;   
    }
     
    fclose($fp_out);
    fclose($fp_in);
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  14. #8
  15. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,897
    Rep Power
    3886
    Even with that solution, you're still using a regexp to see if the line starts with a digit.

    Note this works in Perl and is untested in PHP. I'm not entirely familiar with the difference between their regexp engines.

    Replace this:
    Code:
    (?<!\n)(?=\n\d)
    ... with this:
    Code:
    \n

IMN logo majestic logo threadwatch logo seochat tools logo