Home Man
Search
Today's Posts
Register

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

Tags
perl, shell scripts

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 11-09-2009
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);
# 2  
Old 11-09-2009
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
# 3  
Old 11-09-2009
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 11-09-2009
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
# 5  
Old 11-10-2009
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
# 6  
Old 11-10-2009
Code:
$\ = "\n";             
$/ = "\n\n";
while (<>) {  chomp;  s/\n//g;   print $_ ;}

Login to Reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Multi line log files to single line format ranjancom2000 Shell Programming and Scripting 2 07-16-2018 09:34 AM
Help with reformat single-line multi-fasta into multi-line multi-fasta patrick87 Shell Programming and Scripting 4 06-15-2018 05:30 AM
Remove multi line and single line comments ahmedwaseem2000 UNIX for Dummies Questions & Answers 3 08-29-2013 01:32 AM
Joining multi-line output to a single line in a group gvk25 Shell Programming and Scripting 7 03-07-2012 05:22 AM
Multi lines to single line pandeesh Shell Programming and Scripting 6 12-20-2011 04:28 AM
Merge multi-line output into a single line flagman5 Shell Programming and Scripting 5 10-31-2011 02:15 PM
Transpose multi-line records into a single row daveyabe Shell Programming and Scripting 4 06-16-2011 08:20 PM
How to use Perl to merge multi-line into single line happyday Shell Programming and Scripting 3 05-11-2009 04:42 AM
Merge multi-lines into one single line happyday Shell Programming and Scripting 4 05-09-2009 01:00 PM
Multi-line output to single line LinuxRacr Shell Programming and Scripting 7 02-26-2008 09:05 AM


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

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password