How to Remove Lines if Identical up to Second Comma?
Posted: Sat Jan 28, 2006 7:34 am
I have perl code which works for removing lines in a .CSV(comma-separated values) file identical up to first comma(i.e. when first "key" is identical).
EXAMPLE INPUT (The line numbers are only for identification, they wouldn't be part of the input):
1: 123,abc,XYZ
2: 123,def,UVW
3: 456,abc,XYZ
4: 456,def,UVW
5: 123,abc,QRS
6: 789,abc,XYZ
OUTPUT:
1: 123,abc,XYZ
3: 456,abc,XYZ
6: 789,abc,XYZ
open (FILE,"mycsv.csv");
foreach $line (<FILE>)
{
($first,$second)=split(/,/,$line);
if (!$file{$first})
{
push (@newfile,$line);
$file{$first} = 1;
}
}
print @newfile;
close FILE;
How does one skip line if it is identical up to second comma (if first two keys are identical)?
"TASK 2" EXAMPLE INPUT (The line numbers are only for identification, they wouldn't be part of the input):
1: 123,abc,XYZ
2: 123,def,UVW
3: 456,abc,XYZ
4: 456,def,UVW
5: 123,abc,QRS
6: 789,abc,XYZ
NEEDED OUTPUT:
1: 123,abc,XYZ
2: 123,def,UVW
3: 456,abc,XYZ
4: 456,def,UVW
6: 789,abc,XYZ
Only line 5 should be skipped because "123,abc"(first two keys) were identical in line 1.
Thanks for any help!
j2
EXAMPLE INPUT (The line numbers are only for identification, they wouldn't be part of the input):
1: 123,abc,XYZ
2: 123,def,UVW
3: 456,abc,XYZ
4: 456,def,UVW
5: 123,abc,QRS
6: 789,abc,XYZ
OUTPUT:
1: 123,abc,XYZ
3: 456,abc,XYZ
6: 789,abc,XYZ
open (FILE,"mycsv.csv");
foreach $line (<FILE>)
{
($first,$second)=split(/,/,$line);
if (!$file{$first})
{
push (@newfile,$line);
$file{$first} = 1;
}
}
print @newfile;
close FILE;
How does one skip line if it is identical up to second comma (if first two keys are identical)?
"TASK 2" EXAMPLE INPUT (The line numbers are only for identification, they wouldn't be part of the input):
1: 123,abc,XYZ
2: 123,def,UVW
3: 456,abc,XYZ
4: 456,def,UVW
5: 123,abc,QRS
6: 789,abc,XYZ
NEEDED OUTPUT:
1: 123,abc,XYZ
2: 123,def,UVW
3: 456,abc,XYZ
4: 456,def,UVW
6: 789,abc,XYZ
Only line 5 should be skipped because "123,abc"(first two keys) were identical in line 1.
Thanks for any help!
j2