- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Scripting problem.
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-12-2002 08:22 AM
09-12-2002 08:22 AM
I have a scripting problem I need some help with.
I need to take a file and sort through it and remove records with a duplicate key field. That part is easy, however the hard part is that I need to remove a particular record based on what another field is.
Here is a sample input file:
2443:oliveira:jacquelyn:f:424225:employee:234:ef4442
3465:garvey:thomas:j:554322:employee:235:ef4422
4135:swan:monroe::314642:employee:245:sw4443
4135:swan:monroe::314642:student:245:sf4345
1244:praxel:lynn:m:652424:employee:243:ef4432
1244:praxel:lynn:m:652424:student:243:sf4432
7665:anderson:thomas:j:554322:student:265:sf4422
In this file there are two duplicate people: Swan, and Praxel. I need to keep the employee record if the last field doesn't start with sw.
However if the last field starts with sw, then I need to keep the student record.
So for the above, this is the resulting file I need:
2443:oliveira:jacquelyn:f:424225:employee:234:ef4442
3465:garvey:thomas:j:554322:employee:235:ef4422
4135:swan:monroe::314642:student:245:sf4345
1244:praxel:lynn:m:652424:employee:243:ef4432
7665:anderson:thomas:j:554322:student:265:sf4422
TIA,
Sean
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-12-2002 08:44 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-12-2002 08:49 AM
09-12-2002 08:49 AM
Re: Scripting problem.
open(INP,"
chomp;
@a=split(":",$_);
$code=substr($a[7],0,2);
$ix=$code eq "sw" ? 1 : 0;
$hold{$a[0]}[$ix]=$_;
}
foreach $nbr (sort keys %hold) {
if ($rec=$hold{$nbr}[1]) { print $rec,"\n"; }
else { print $hold{$nbr}[0],"\n"; }
}
HTH
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-12-2002 08:49 AM
09-12-2002 08:49 AM
Re: Scripting problem.
2443:oliveira:jacquelyn:f:424225:employee:234:ef4442
3465:garvey:thomas:j:554322:employee:235:ef4422
4135:swan:monroe::314642:employee:245:sw4443
4135:swan:monroe::314642:student:245:sf4345
1244:praxel:lynn:m:652424:employee:243:ef4432
1244:praxel:lynn:m:652424:student:243:sf4432
7665:anderson:thomas:j:554322:student:265:sf4422
l1:/tmp 178 > perl -naF: -e'push@{$e{$F[1]}},[@F]}END{$,=":";for(keys%e){@x=@{$e{$_}};@x>1 and@x=grep{$_->[-1]!~/^sw/}@x;@x>1 and@x=grep{$_->[-3]=~/^e/}@x;print@$_ for@x}' xx.txt
3465:garvey:thomas:j:554322:employee:235:ef4422
4135:swan:monroe::314642:student:245:sf4345
1244:praxel:lynn:m:652424:employee:243:ef4432
7665:anderson:thomas:j:554322:student:265:sf4422
2443:oliveira:jacquelyn:f:424225:employee:234:ef4442
l1:/tmp 179 >
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-12-2002 09:08 AM
09-12-2002 09:08 AM
Re: Scripting problem.
perl -naF: -e '{next if $t{$F[0]} eq "sw";$h{$F[0]}=$_;$t{$F[0]=substr($F[7],0,2)}END{print values %h}'
This collects each record into $h{} keys on id number (1st field) until a "sw" is found, then no more is collected on the id number. The output will not be in any sorted order.
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-12-2002 09:35 AM
09-12-2002 09:35 AM
Re: Scripting problem.
What do you want to do when the ONLY record has in the last field a string that starts with "sw" ?? Like if the following record was in the file (NO duplicate):
4465:duck:donald::884712:student:239:sw6969
live free or die
harry
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-12-2002 09:40 AM
09-12-2002 09:40 AM
Re: Scripting problem.
Harry, my solution already catches that
If for field [1] (the name) there is only one record, print it. If there is more than one, filter out the records where the last field starts with sw. If there still more than one record for that key, use the employee one.
which yielded what he wanted
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-12-2002 09:58 AM
09-12-2002 09:58 AM
Re: Scripting problem.
perl -naF: -e '{
next if defined($t{$F[0]}) && $t{$F[0]} ne "sw";
$h{$F[0]}=$_;
$t{$F[0]}=substr($F[7],0,2);
}
END{print values %h}'
Tom
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-12-2002 09:58 AM
09-12-2002 09:58 AM
Re: Scripting problem.
In theory there should NEVER be only a record with a sw field.
But in any case if there is only one record for the ID, then that record needs to be kept.
The basis of this is a student file and an employee file being combined into a unique file.
It is possible that a person could be an actual employee and student, in which case we want the employee record. It's also possible that a person could be a student and student worker (employee) in which case we want the student record.
Anyone in only one of the two files should be kept.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-12-2002 10:00 AM
09-12-2002 10:00 AM
Re: Scripting problem.
Both of my solutions handle that situtation. It grabs whatever records are available into a hash variable and then dumps out what was collected (minus duplicates and keeping "sw" even if it the only record for that id).
Syntax fix to my previous one liner-
perl -naF: -e '{next if $t{$F[0]} eq "sw";$h{$F[0]}=$_;$t{$F[0]}=substr($F[7],0,2)}END{print values %h}' xx.txt
(forgot a closing '}')...
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-19-2002 07:07 AM
09-19-2002 07:07 AM
Re: Scripting problem.
I didn't get a chance to test all of your solutions, so everyone gets 8 points for their efforts.
Thanks a lot for the help.
Sean