in reply to Re: perl regex to match newline followed by number and text
in thread perl regex to match newline followed by number and text

I guess something very, very simple like this is possible? 3 input lines to one output line?
Good idea, but why then still use a regex?
while (<DATA>) { chomp; print; print $. % 3 ? " " : "\n"; } __DATA__ 99~Arun~Kumar~Mobilenum: 1234-567 , from Earth Human 98~Mahesh~Babu~Mobilenum: 5678-901 , from Earth Human
Output
99~Arun~Kumar~Mobilenum: 1234-567 , from Earth Human 98~Mahesh~Babu~Mobilenum: 5678-901 , from Earth Human


holli

You can lead your users to water, but alas, you cannot drown them.

Replies are listed 'Best First'.
Re^3: perl regex to match newline followed by number and text
by arunkumarzz (Novice) on Jun 01, 2019 at 18:07 UTC
    The number of newlines is not fixed, it might be one or two or three or more newlines in the last field
      it might be one or two or three or more newlines in the last field
      Well it'd be nice if your sample data would reflect that. Anyhow. I'd solve this using a simple algorithm rather than a complicated regex that backtracks god knows how often with all these .*
      Consider:
      use warnings; use strict; my @record; while (<DATA>) { if (/^\d+\~/ && @record) { print join (" ", @record), "\n"; @record = (); } chomp; push @record, $_; } print join (" ", @record), "\n"; __DATA__ 99~Markus~Holli~Mobilenum: 1234-567 , from Earth Europe White Human 98~Mahesh~Babu~Mobilenum: 5678-901 , from Earth India Brown Human
      Output
      99~Markus~Holli~Mobilenum: 1234-567 , from Earth Europe White Human 98~Mahesh~Babu~Mobilenum: 5678-901 , from Earth India Brown Human


      holli

      You can lead your users to water, but alas, you cannot drown them.