[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: [K12OSN] script help please (OT)



Here's another way to do it, with perl:

#!/usr/bin/perl
%hash = ();
while (<>) {
  chomp $_;
  ($part1,$part2) = split /,/, $_, 2;
  if ( ! $hash{$part1} ) {
    $hash{$part1} = $part1.",".$part2;
    next;
  } else {
  $hash{$part1} = $hash{$part1}.",".$part2;
  }
}
foreach $record (sort keys %hash) {
  print "$hash{$record}\n";
}


This script deals with the problem that some other suggestions missed I think, that when you append the values from line 2 you have to add another comma at the end of line 1, and then add another comma at the end of that before you add line 3, and another comma after that before you add line 4; but then don't add a comma at the end of line 4. If you put the above into a file, make it executable, and then call it followed by the name of the data file, it will print out the result. Assuming you get what you want, you can then run it again and redirect the output to a file.


Petre

Jimmy Schwankl wrote:
Hey folks,

I know this won't be much of a challenge for you script gurus, but my skills are woefully inadequate.

I have a user who has to aggregate some data that was given to him in a CSV file. The problem is that the data for each record is split across four lines, and the first item on each line is identical.

What he would like is for the first line to have the next three lines appended to it with the first item stripped out so that when he imports the file into a spreadsheet all the data for each record is on one line.

For example:

19550,"I","2004-2005","190","304",2004-09-24,"N","N"
19550,"O",1,"01","First","Last","S","10304012","M","W", 1992-07-05,12,"07","01","N","6","N","N","1"
19550,"A",1,"42","18","","",""
19550,"C",1,"003","","1",2004-09-24,"Principal Name","","","","","","","",""
19567,"I","2004-2005","190","304",2004-10-01,"N","N"
19567,"O",1,"01","First2","Last2","S","10304012","M","W", 1992-07-05,12,"07","01","N","6","N","N","1"
19567,"A",1,"36","18","","",""
19567,"C",1,"003","","1",2004-10-04,"Principal Name","","","","","","","",""
19581,"I","2004-2005","190","304",2004-10-04,"N","N"
19581,"O",1,"01","First3","Last3","S","970402","M","B", 1992-08-31,12,"06","01","N","6","N","N","1"
19581,"A",1,"32","18","","",""
19581,"C",1,"003","","1",2004-10-05,"Principal Name","","","","","","","",""


so basically remove the 2nd, 3rd, and 4th instances of 19550 and add the result onto the end of the first line.
remove the 2nd, 3rd, and 4th instances of 19567 (6th, 7th, and 8th lines) and add the result onto the end of the fifth line.
etc.


result:

19550,"I","2004-2005","190","304",2004-09-24,"N","N","O", 1,"01","First","Last","S","10304012","M","W", 1992-07-05,12,"07","01","N","6","N","N","1","A", 1,"42","18","","","","C",1,"003","","1",2004-09-24,"Principal Name","","","","","","","",""
19567,"I","2004-2005","190","304",2004-10-01,"N","N","O", 1,"01","First2","Last2","S","10304012","M","W", 1992-07-05,12,"07","01","N","6","N","N","1","A", 1,"36","18","","","","C",1,"003","","1",2004-10-04,"Principal Name","","","","","","","",""
19581,"I","2004-2005","190","304",2004-10-04,"N","N","O", 1,"01","First3","Last3","S","970402","M","B", 1992-08-31,12,"06","01","N","6","N","N","1","A", 1,"32","18","","","","C",1,"003","","1",2004-10-05,"Principal Name","","","","","","","",""


If anyone can spare a few minutes or would enjoy showing off their scripting skills, I'd appreciate it.

Thank you,
Jimmy Schwankl


+++++++++++++++++++++++++++++++++++++++++++

Always proofread carefully to see if you any words out.


**This message was sent from the Chatham County Schools Mail Server**
*****All e-mail correspondence to and from this address is subject to the North Carolina Public Records Law, which may result in monitoring and disclosure to third parties, including law enforcement.*****


_______________________________________________
K12OSN mailing list
K12OSN redhat com
https://www.redhat.com/mailman/listinfo/k12osn
For more info see <http://www.k12os.org>



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]