Join Function in PERL


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Join Function in PERL
# 1  
Old 06-04-2010
Join Function in PERL

Hi,

Can any one please let me know, how to join the lines in a file, but based one a condition.

There is a file, where few lines start with a date stamp. and few do not.

I wanted to join the lines till I find a date stamp. If found date its should in a newline.

Please help me.

Thanks in Advance
# 2  
Old 06-04-2010
Code:
perl -ne 'chomp;if (/TIMESTAMP/){$_="\n" . $_;$\="\n";}print;' in.file > out.file

# 3  
Old 06-04-2010
Thanks...bartus11, it didnt worked.. please help

Hey bartus11

I tried as below with the code you gave, but it dint worked can you please let me know where am doing a mistake.

#!/usr/bin/perl
use strict;
my $dt=`date +%Y-%m-%d`;
chomp $dt;

my $inp_file=""/home/skMessages.log";

{
chomp $inp_file;
open (INFILE,"$inp_file") || die "Cant open $inp_file for reading ";

while (<INFILE>)
{
if ($_ =~ $dt)
{
$_="\n" . $_;$\="\n";
print;
}
}
}
close(INFILE);

---------- Post updated at 11:55 AM ---------- Previous update was at 11:53 AM ----------

Quote:
Originally Posted by thankful123
Hey bartus11

I tried as below with the code you gave, but it dint worked can you please let me know where am doing a mistake.
Code:
#!/usr/bin/perl
use strict;
my $dt=`date +%Y-%m-%d`;
chomp $dt;
 
my $inp_file=""/home/skMessages.log";
 
{
chomp $inp_file;
open (INFILE,"$inp_file") || die "Cant open $inp_file for reading ";
 
while (<INFILE>)
{
if ($_ =~ $dt)
{
$_="\n" . $_;$\="\n";
print;
}
}
}
close(INFILE);

# 4  
Old 06-04-2010
Code:
#!/usr/bin/perl
use strict;
my $dt=`date +%Y-%m-%d`;
chomp $dt;
my $inp_file="/home/skMessages.log";
open (INFILE,"$inp_file") || die "Cant open $inp_file for reading ";
while (chomp ($_=<INFILE>)){
  if ($_ =~ $dt){
    $_="\n" . $_;
    $\="\n";
  }
  print;
}
close(INFILE);

This User Gave Thanks to bartus11 For This Post:
# 5  
Old 06-04-2010
Its printing in a same line, all the file, the joining is not breaked if found date stamp.

2009-11-10 04:49:16,170 INFO handlers
b;
c;
ddaa;

2009-11-10 04:49:16,170 INFO handlers
aa;
bb;
cc;

OUTPUT:

2009-11-10 04:49:16,170 INFO handlers b; c; ddaa;
2009-11-10 04:49:16,170 INFO handlers aa; bb; cc;

Sorry for the confusion, if any.

Thanks
# 6  
Old 06-04-2010
Quote:
Originally Posted by thankful123
Its printing in a same line, all the file, the joining is not breaked if found date stamp.

2009-11-10 04:49:16,170 INFO handlers
b;
c;
ddaa;

2009-11-10 04:49:16,170 INFO handlers
aa;
bb;
cc;

OUTPUT:

2009-11-10 04:49:16,170 INFO handlers b; c; ddaa;
2009-11-10 04:49:16,170 INFO handlers aa; bb; cc;

...
Something like this maybe ?

Code:
$
$
$ cat f1
2009-11-10 04:49:16,170 INFO handlers
b;
c;
ddaa;

2009-11-10 04:49:16,170 INFO handlers
aa;
bb;
cc;
$
$
$ perl -ne 'chomp; if (/^\d{4}-\d\d-\d\d \d\d:\d\d:\d\d,.*$/ or /./){print $_," "} else {print "\n"} END {print "\n"}' f1
2009-11-10 04:49:16,170 INFO handlers b; c; ddaa;
2009-11-10 04:49:16,170 INFO handlers aa; bb; cc;
$
$

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Converting shell to Perl I run into shell built in function trap and need alternative in Perl

I am working on converting shell to Perl script. In shell we have built in function trap Do you know alternative in Perl or actually we don't need it? Thanks for contribution (3 Replies)
Discussion started by: digioleg54
3 Replies

2. Shell Programming and Scripting

Problem of Perl's "join" function

$ perl -e '@f=("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","1","911"); print join("\t",@f)."\n";' aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ... (5 Replies)
Discussion started by: carloszhang
5 Replies

3. UNIX for Dummies Questions & Answers

How to use the the join command to join multiple files by a common column

Hi, I have 20 tab delimited text files that have a common column (column 1). The files are named GSM1.txt through GSM20.txt. Each file has 3 columns (2 other columns in addition to the first common column). I want to write a script to join the files by the first common column so that in the... (5 Replies)
Discussion started by: evelibertine
5 Replies

4. Shell Programming and Scripting

Perl split and join

Hi, I have tried the split and join functions but stuck with unexpected results. Any help appreciated. I pass multiple values at command line like perl test.pl -type java,xml. This works good for me but i am not sure how to print it in the required format. Here is the code i tried:... (4 Replies)
Discussion started by: nmattam
4 Replies

5. Shell Programming and Scripting

Function extraction in PERL

the log contains mathematical operation as follows fm_void_mathematics : PCM_OP_MATHS input function PIN_FLD_NUM1 INT 1 PIN_FLD_NUM2 INT 2 PIN_FLD_RESULTS int PIN_FLD_OUT INT * D Wed Sep 16 05:40:22 2009 solaris_testing fm_void_add : PIN_FLD_SUM int 3 D Wed Sep 16 05:40:22 2009... (1 Reply)
Discussion started by: vkca
1 Replies

6. UNIX for Dummies Questions & Answers

Join 2 files with multiple columns: awk/grep/join?

Hello, My apologies if this has been posted elsewhere, I have had a look at several threads but I am still confused how to use these functions. I have two files, each with 5 columns: File A: (tab-delimited) PDB CHAIN Start End Fragment 1avq A 171 176 awyfan 1avq A 172 177 wyfany 1c7k A 2 7... (3 Replies)
Discussion started by: InfoSeeker
3 Replies

7. Programming

sql,multiple join,outer join issue

example sql: select a.a1,b.b1,c.c1,d.d1,e.e1 from a left outer join b on a.x=b.x left outer join c on b.y=c.y left outer join d on d.z=a.z inner join a.t=e.t I know how single outer or inner join works in sql. But I don't really understand when there are multiple of them. can... (0 Replies)
Discussion started by: robbiezr
0 Replies

8. Shell Programming and Scripting

oct function in perl

hi i came across one program which uses some method to calculate file permissions using lstat in perl and i am not getting how its doing that i am pasting the code below ... $ > cat b.pl use Fcntl':mode'; my... (0 Replies)
Discussion started by: zedex
0 Replies

9. Shell Programming and Scripting

join 2 array in perl?

Hi guys, Can anyone tell me how to join 2 array together? @array1=("abc", "def"); @array2=("xyz", "uuu"); @join_array = fun(@array1,@array2); # @join_array == ("abc", "def", "xyz", "uuu") any function can do what fun(@@) does? thanks Gusla (2 Replies)
Discussion started by: gusla
2 Replies

10. UNIX for Dummies Questions & Answers

join function and -o and -a

I'm completing a join where I specify the outfile format....such as: join -t \| -j1 1 -j2 1 -o 1.1 1.2 1.3 2.4 1.5 1.6 1.7 $OUTFILE TMP_INFILE > NEW_OUTFILE Now this works great. Now what I also want is to include any unmatched rows from the first file...and I use the -a flag for this. ... (1 Reply)
Discussion started by: peter.herlihy
1 Replies
Login or Register to Ask a Question