[CDBUG-talk] Weird scripting question

Justin Sherrill justin at shiningsilence.com
Fri Feb 10 20:37:27 EST 2017


I can't immediately think of an awk solution but a perl one is easy.
This could probably be reduced by a line or two with a few minutes of
concentrated thought.

#!/usr/local/bin/perl

while (<>)
{
    ($first,$last,$id,$school) = split /,/,$_;
    $all_ids{$id}++;
    if ($all_ids{$id} gt 1) { $id = $id . '-' . "$all_ids{$id}"; }
    print "$first,$last,$id,$school";
}

On Fri, Feb 10, 2017 at 12:56 PM, Jaime <jaime at snowmoon.com> wrote:
> I have a weird scripting question.  I have two vendors:  One is providing a
> CSV file of people, their ID#s, the building where they work, etc.;  the
> other is requesting a CSV file of that data in a different format.  I'm
> currently doing the conversion between these two formats via awk inside a
> bash script.  (The script also grabs the first file over SFTP and uploads
> the post-processed file to another server via SFTP.)
>
> So I have a data file from the first vendor that says:
> Fred,Flinstone,111,High School
> Fred,Flintstone,111,Middle School
> Betty,Rubble,112,MiddleSchool
>
> I need to send it to the vendor that automates emergency phone calls (in the
> event of closure due to snow and things like that.)  The second vendor will
> only list Fred Flintstone as working in the Middle School, since the two
> lines have the same employee ID#.
>
> I was told by the second vendor to change the lines to be more like:
> Fred,Flintstone,a111,High School
> Fred Flintstone,b111,Middle School
> Betty,Rubble,112,Middle School
>
> Betty has the same ID# because there is only one record for her.  Fred gets
> "a" and "b" prepended because there are two entries and we need to be sure
> they have different ID#s.
>
> Any idea how to do this?  Is it even possible with awk?
>
> Jaime
>
> _______________________________________________
> CDBUG-talk mailing list
> CDBUG-talk at lists.nycbug.org
> http://lists.nycbug.org/mailman/listinfo/cdbug-talk



More information about the CDBUG-talk mailing list