Hello, I am fairly new to Perl, and I have been trying to use some regular expressions in a Perl one liner to replace \r\n (or \n on Unix) with a single space, thereby joining lines. It is however a bit more involved than that because this joining should only happen under certain conditions. Here is the kind of text files that I am dealing with:

line de Bitte lesen Sie die Gebrauchsanweisung gut durch und bewahren Sie diese auf. en Please read these instructions carefully and keep for future reference. fr Lire et conserver ces instructions pour toute utilisation future du produit. line de 19 Zubehoerteile. en 19 Accessories. fr 19 Accessoires line de 10 Aktivitaeten. en 10 Activities. fr 10 Activite's.

I have a whole list of text files with text in German, French and English. All lines containing meaningful text are preceded by 4 spaces. However, I would like to have only a single line of text under each language for those language lines that space multiple lines (a line being 76 characters long maximum at present). In other words, the above would become:

line de Bitte lesen Sie die Gebrauchsanweisung gut durch und bewahren Sie +diese auf. en Please read these instructions carefully and keep for future refer +ence. fr Lire et conserver ces instructions pour toute utilisation future d +u produit. line de 19 Zubehoerteile. en 19 Accessories. fr 19 Accessoires line de 10 Aktivitaeten. en 10 Activities. fr 10 Activite's.

The languages text that already span only 1 line are left untouched, whereas the other lines, where the language text spans more than 1 lines, are joined. I have tried several regular expressions, all of them as Perl one liners as I thought it would be an extra challenge to do it all one 1 line, such as

perl -i.bak -pe 's/^\s\s\s\s(.*)$\n\s\s\s\s/$1/g' myfile.txt perl -i.bak -pe 's/\n\s\s\s\s/ /gm' myfile.txt

However, none of my regular expressions works. It looks as if anything specified after the \n is ignored. Any idea? Any help would be immensely appreciated? Thank you very much

In reply to replace \n with space to join indented lines by olivier

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.