Removing windows CR-LF from middle of text

Discussion in 'Perl Misc' started by v796, May 5, 2004.

  1. v796

    v796 Guest

    Hi,
    I have text on multiple lines in text files on Windows. Example:
    " Your voting instructions have been
    received.
    Thank you! "

    I need a regex to remove CR-LF and convert them to space in the middle
    of the text. I tried this regex which works at the end.
    $content =~ s/^([\s\S]+\S)\s*$/$1/;

    This works to remove all CR-LF of Windows at the end an puts into $1.
    I must remove the CR-LF in the middle too. How can this be done?

    My script is running on Linux.
    Finally, I have to import $content into a single column in Excel file.
    Unfortunately, the excel file requirement just came up yesterday, else
    I would have used CPAN WriteExcel module. Oh well!

    Currently the text in excel writes to multiple lines in Excel file,
    when it should only be in 1 cell.

    Any comments are appreciated.

    Thanks,
    vk
    v796, May 5, 2004
    #1
    1. Advertising

  2. v796 wrote:

    > Hi,
    > I have text on multiple lines in text files on Windows. Example:


    > I need a regex to remove CR-LF and convert them to space in the middle
    > of the text. I tried this regex which works at the end.
    > $content =~ s/^([\s\S]+\S)\s*$/$1/;

    Your re doesn't do this at all: it's merely stripping whitespace from the end of lines that end
    with non-whitespace followed by whitespace. [\s\S]+ (match white-space or non-whitespace, one or
    more times) will pretty much match anything. You could try dropping the end-of line anchor.

    eg

    $content =~ s/\r\n/ /g;

    > This works to remove all CR-LF of Windows at the end an puts into $1.
    > I must remove the CR-LF in the middle too. How can this be done?


    > Finally, I have to import $content into a single column in Excel file.
    > Unfortunately, the excel file requirement just came up yesterday, else
    > I would have used CPAN WriteExcel module. Oh well!

    how does this prevent you from using it? It isn't going to be quicker to implement it from scratch?

    > Currently the text in excel writes to multiple lines in Excel file,
    > when it should only be in 1 cell.

    what text in excel? what are you trying at the moment?

    I suggest you write the output to a csv and get excel to import it, or using DBD::Excel. Failing
    all that you could look at running Win32::OLE under Windows. You need to learn how to use CPAN...

    regards,

    Mark
    Mark Clements, May 5, 2004
    #2
    1. Advertising

  3. v796

    Joe Smith Guest

    v796 wrote:

    > I have text on multiple lines in text files on Windows. Example:
    > " Your voting instructions have been
    > received.
    > Thank you! "


    From what I've seen, text like that is really
    " Your voting instructions have been\n received.\nThank you!"\r\n
    in that the soft line breaks are bare LF and hard returns are CRLF.

    This becomes visible when using
    od -c whatever.csv
    at the Unix/Linux command line.

    > I need a regex to remove CR-LF and convert them to space in the middle
    > of the text.


    I've had some success with this:

    binmode IN; # Preserve CR in input stream
    local $\ = "\r\n"; # Read DOS-style records
    while (<IN>) {
    s/\n/ /gs; # Convert soft breaks into spaces
    s/\r /\n/; # Convert what used to be CRLF to plain LF
    ...
    }

    > Currently the text in excel writes to multiple lines in Excel file,
    > when it should only be in 1 cell.


    The above code recognizes that as a single cell on a line.

    Of course, using a CPAN module to parse CSV would be better.
    -Joe
    Joe Smith, May 5, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kay
    Replies:
    2
    Views:
    7,149
  2. brucie
    Replies:
    4
    Views:
    13,274
    Toby A Inkster
    Jul 10, 2004
  3. Floydster

    Text in the middle of a web page

    Floydster, Aug 29, 2005, in forum: HTML
    Replies:
    3
    Views:
    730
    Floydster
    Aug 31, 2005
  4. Chris  Chiasson
    Replies:
    6
    Views:
    615
    Richard Tobin
    Nov 14, 2006
  5. Rick Csucsai

    how to insert text into middle of textarea

    Rick Csucsai, Jun 21, 2005, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    266
    Harolds
    Jun 24, 2005
Loading...

Share This Page