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!

*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.

Jobs

Concatenate records based on first column in each record

Concatenate records based on first column in each record

Concatenate records based on first column in each record

(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

I have this script:

BEGIN { RS = "\n"; FS = " "}
{
prev = ""
oneof = ""
print "mrn: ", $1
print "acct: ", $2
print "admit date: ", $3
print "admit time: ", $4
print ""

$1!=prev
{
prev = $1
oneof = ""
}

if ( $1 && $1 == prev )
{
oneof=oneof$1","$2","
print "oneof: ", oneof
next
}
}
I get the following output:
111112,4000000001, and so on

I'm messing up on my logic. prev is supposed to keep track of the current customer id as mrn changes. As long as prev equals mrn you concatenate all the accounts. When the customer id changes, change prev to the new mrn then start concatenation again. Maybe someone can point me in the right direction to get the correct format.


RE: Concatenate records based on first column in each record

(OP)
Thanks PHV. This helped a lot. I added on some things because I made some changes. I'm new to awk but I use perl, ksh, and sed so it's just some awk concepts I need to learn then the syntax. AWK seems to be a very useful tool to add to the arsenal. I plan use it more often in the future.

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!

Resources

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