×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Concatenate based on 1st column in a file of records

Concatenate based on 1st column in a file of records

Concatenate based on 1st column in a file of records

(OP)
I have a file with records such as:

111112 4000000001 01/01/13 13:05
111112 4000000002 02/02/13 13:05
111112 4000000003 03/03/13 13:05
111113 4000000004 04/01/13 07:05
111113 4000000005 05/01/13 10:05
111113 4000000006 06/01/13 17:05
111114 4000000007 06/01/13 18:05
111114 4000000008 06/01/13 19:05
111114 4000000009 06/01/13 20:05

1st column is an customer id, 2nd column is an acct. no.

I would like it in this format:

111112,4000000001,4000000002,4000000003
111113,4000000005,4000000005,4000000006
111114,4000000007,4000000008,4000000009

here's my perl:

use diagnostics;
use strict;
use warnings;
my $line;
my $str;
my $prev="";
my $cur="";
my @accts=("");
open(INPUT, "sample.txt") || die("Error opening file for reading");

while($line = <INPUT>)
{
#print $line;
my @rec = split /\s+/, $line;
#$cur = $rec[1];
print "id: $rec[1]\n";
print "acct: $rec[2]\n";
print "admit_date: $rec[3]\n";
print "admit_time: $rec[4]\n";

if ( $rec[1] ne $prev )
{
$prev = $cur;

}

if( $rec[1] eq $prev )
{
push(@accts, $rec[2]);
print "@accts\n";
$cur = "";
}

print "\nprev: $prev\n";

}
print "\n\nScript finished!!!";

The concatenation does happen but it happens progressively unitl at the end of the while loop all accounts are concatenated instead of getting the correct output specified above. I know I'm close but my logic is off. Perhaps someone can point me in the right direction.

RE: Concatenate based on 1st column in a file of records

From your example it is unclear what do you want for something like

CODE -->

111112 4000000001 01/01/13 13:05
111112 4000000002 02/02/13 13:05
111113 4000000003 04/01/13 07:05 
111113 4000000004 05/01/13 10:05 
111112 4000000005 03/03/13 13:05
111113 4000000006 06/01/13 17:05 
111114 4000000007 06/01/13 18:05 
111114 4000000008 06/01/13 19:05 
111114 4000000009 06/01/13 20:05 
Do you need to collect all the lines for the same id or only the consecutive ones?
In the first case you need to read the whole file into a hash:

CODE -->

while(<INPUT>){
  @rec=split;
  push @{$myhash{$rec[0]}},$rec[1];
}
for(sort keys %myhash){
  print join(',',@{$myhash{$_}}),"\n";
} 

http://www.xcalcs.com : Online engineering calculations
http://www.megamag.it : Magnetic brakes for fun rides
http://www.levitans.com : Air bearing pads

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close