Operating System - HP-UX
1753283 Members
5446 Online
108792 Solutions
New Discussion юеВ

Re: Making 5 lines into 1

 
SOLVED
Go to solution
H.Merijn Brand (procura
Honored Contributor

Re: Making 5 lines into 1

perl -e 'm/^I have/ and print ($x),$x="";chomp$x;$x.=" $_";END{print$x}' datafile
Enjoy, Have FUN! H.Merijn
Robin Wakefield
Honored Contributor

Re: Making 5 lines into 1

Hi,

Just a quick note on the above, it should be:

perl -ne ......

for auto-looping.

Rgds, Robin.
H.Merijn Brand (procura
Honored Contributor

Re: Making 5 lines into 1

perl -ne 'm/^I have/ and print ($x),$x="";chomp$x;$x.=" $_";}END{print$x' datafile

/and/ the braces were wrong.
To explain: perl -ne is nothing more than a wrapper round the -e part like

while (<>) {
... your -e part here ...
} continue {
print;
}

When you know this, end you also know how END blocks are parsed, my script will expand to

while (<>) {
m/^I have/ and print ($x), $x = "";
chomp $x;
$x .= " $_";
}
END {
print $x
} continue {
print;
}
Enjoy, Have FUN! H.Merijn