I need to pick out the first, last and how many from a file...?!!?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
From this file I have to write a script that does the following things AND they have to be done from a script written for the bash shell:
1) Based on the last 2 arguments "JOE,0015557" figure out how many times it comes up in the file.
2) From the ones that occur more than once, when was the first and when was the last time it occured. This you figure out based on the first field which is a timestamp.
So the file that has to come as output from the bash scipt will be something like:
try using cut and use as your delimiter the ',' asign fields 2 and 3 to a variable and loop throgh the file, if you have a counter variable whenever you find a match you increase the counter
#!/usr/bin/perl
while(<STDIN>) {
chomp;
($date,$user,$number) = split /,/;
$key = "$user,$number";
$frec{$key}++;
if ($first{$key}) {
$first{$key} = $date if ($first{$key} ge $date);
} else {
$first{$key} = $date;
}
if ($last{$key}) {
$last{$key} = $date if ($last{$key} le $date);
} else {
$last{$key} = $date;
}
}
for $i (keys %frec) {
if ($frec{$i}>1) {
print "$i---$frec{$i}---First incidende:$first{$i}----Last incidende:$last{$i}\n";
}
}
If you save a file named "yourscript" with this and give it excecution permissions, you can run
Code:
cat yourfile.txt | yourscript
PS: I'm very new to perl, but from what I now of its, there is plenty of other ways to write this problem, many of them better than mine. Anyway, at least I can explain you how this solution works, so dont hesitate to ask...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.