Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

How to use Perl to join multi-line into single line

Shell Programming and Scripting


Tags
perl

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 11-09-2009   -   Original Discussion by happyday
happyday happyday is offline
Registered User
 
Join Date: May 2009
Last Activity: 18 November 2009, 3:49 AM EST
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
How to use Perl to join multi-line into single line

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);
Sponsored Links
    #2  
Old Unix and Linux 11-09-2009   -   Original Discussion by happyday
durden_tyler's Unix or Linux Image
durden_tyler durden_tyler is offline Forum Advisor  
Registered User
 
Join Date: Apr 2009
Last Activity: 9 September 2017, 1:30 PM EDT
Posts: 2,083
Thanks: 21
Thanked 383 Times in 346 Posts
Here's one way to do it with Perl:


Code:
$
$ 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
Sponsored Links
    #3  
Old Unix and Linux 11-09-2009   -   Original Discussion by happyday
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
 
Join Date: Jun 2007
Last Activity: 11 November 2016, 3:44 AM EST
Location: Beijing China
Posts: 1,305
Thanks: 0
Thanked 26 Times in 26 Posts
perl:

Code:
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:

Code:
sed -n '/^$/!{
${H;x;s/\n//g;p;}
$!{H;}
}
/^$/{x;s/\n//g;p;d;}' yourfile.txt | sed '/^$/d'

    #4  
Old Unix and Linux 11-10-2009   -   Original Discussion by happyday
daptal daptal is offline
Registered User
 
Join Date: Mar 2009
Last Activity: 18 July 2012, 9:53 PM EDT
Posts: 175
Thanks: 0
Thanked 2 Times in 2 Posts

Code:
 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;


Code:
 perl -e 'my $ln=0;
             while(<>){ 
                chomp;
                print "\n"if (m/^timestamp/ && $ln);
                print"$_";
                $ln++;}
print"\n" '

HTH,
PL
Sponsored Links
    #5  
Old Unix and Linux 11-10-2009   -   Original Discussion by happyday
durden_tyler's Unix or Linux Image
durden_tyler durden_tyler is offline Forum Advisor  
Registered User
 
Join Date: Apr 2009
Last Activity: 9 September 2017, 1:30 PM EDT
Posts: 2,083
Thanks: 21
Thanked 383 Times in 346 Posts
And yet another:


Code:
$
$ 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
Sponsored Links
    #6  
Old Unix and Linux 11-10-2009   -   Original Discussion by happyday
ghostdog74 ghostdog74 is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 28 January 2015, 8:30 AM EST
Posts: 2,669
Thanks: 0
Thanked 18 Times in 18 Posts

Code:
$\ = "\n";             
$/ = "\n\n";
while (<>) {  chomp;  s/\n//g;   print $_ ;}

Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Join in a single line variable number of lines cgkmal Shell Programming and Scripting 4 08-03-2009 07:52 AM
Help on Merge multi-lines into one single line happyday Shell Programming and Scripting 5 05-25-2009 08:30 AM
How to use Perl to merge multi-line into single line happyday Shell Programming and Scripting 3 05-11-2009 05:42 AM
Merge multi-lines into one single line happyday Shell Programming and Scripting 4 05-09-2009 02:00 PM
Multi-line output to single line LinuxRacr Shell Programming and Scripting 7 02-26-2008 10:05 AM



All times are GMT -4. The time now is 01:07 AM.