Hello,
Did anyone know how to write a perl script to merge the multi-line into a single line where each line with start at timestamp
Input-->
timestamp=2009-11-10-04.55.20.829347;
a;
b;
c;
timestamp=2009-11-10-04.55.20.829347;
aa;
bb;
cc;
Output-->
timestamp=2009-11-10-04.55.20.829347;a;b;c;
timestamp=2009-11-10-04.55.20.829347;aa;bb;cc;
Thank You
HappyDay
---------- Post updated at 01:33 AM ---------- Previous update was at 01:31 AM ----------
Hello,
I had this script but can't work
# read "input.txt"
open (FILE, 'org.log') or die "$!";
open (NEWFILE, '> joinline.txt') or die "$!";
$line = '';
while (<FILE>) {
chomp($);
if ($ ne '') {
$line = $line . "" . $_;
} else {
print "$line\n";
print NEWFILE "$line\n";
$line = '';
}
}
close (FILE);
close (NEWFILE);
Here's one way to do it with Perl:
$
$ cat -n f6
1 timestamp=2009-11-10-04.55.20.829347;
2 a;
3 b;
4 c;
5
6
7 timestamp=2009-11-10-04.55.20.829347;
8 aa;
9 bb;
10 cc;
$
$ perl -lne 'BEGIN{undef $/} s/\n//g; s/;(timestamp)/;\n$1/g; print' f6
timestamp=2009-11-10-04.55.20.829347;a;b;c;
timestamp=2009-11-10-04.55.20.829347;aa;bb;cc;
$
$
tyler_durden
perl:
local $/="\n\n\n";
while(<DATA>){
s/\n//g;
print $_,"\n";
}
__DATA__
timestamp=2009-11-10-04.55.20.829347;
a;
b;
c;
timestamp=2009-11-10-04.55.20.829347;
aa;
bb;
cc;
sed:
sed -n '/^$/!{
${H;x;s/\n//g;p;}
$!{H;}
}
/^$/{x;s/\n//g;p;d;}' yourfile.txt | sed '/^$/d'
daptal
November 9, 2009, 11:48pm
4
cat abc.txt
timestamp=2009-11-10-04.55.20.829347;
a;
b;
c;
ddaa;
timestamp=2009-11-10-04.55.20.829347;
aa;
bb;
cc;
perl -e 'my $ln=0;
while(<>){
chomp;
print "\n"if (m/^timestamp/ && $ln);
print"$_";
$ln++;}
print"\n" '
HTH,
PL
And yet another:
$
$ cat -n f6
1 timestamp=2009-11-10-04.55.20.829347;
2 a;
3 b;
4 c;
5
6
7 timestamp=2009-11-10-04.55.20.829347;
8 aa;
9 bb;
10 cc;
11
12
13
14
15
16
17 timestamp=2009-11-10-04.55.20.829347;
18 aaa;
19 bbb;
20 ccc;
21
22 timestamp=2009-11-10-04.55.20.829347;
23 aaaa;
24 bbbb;
25 cccc;
$
$ perl -lne 'chomp; if(/./){$s.=$_; $x=1} elsif(/^$/ && $x){print $s; $s=""; $x=0} END{print $s}' f6
timestamp=2009-11-10-04.55.20.829347;a;b;c;
timestamp=2009-11-10-04.55.20.829347;aa;bb;cc;
timestamp=2009-11-10-04.55.20.829347;aaa;bbb;ccc;
timestamp=2009-11-10-04.55.20.829347;aaaa;bbbb;cccc;
$
$
tyler_durden
$\ = "\n";
$/ = "\n\n";
while (<>) { chomp; s/\n//g; print $_ ;}