How can I sort out the first and last of a list of uniques?!?!!
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.
1- Based on the 2nd and 3rd fields (thus for the first entry it would be this for example: "RED,54343717" <---so this would be used as the unique identifier) I would like to know 3 things:
a. How many occurances are of each (regardless of the first date field obviously).
b. When was the first time it came up.
c. When was the last time it came up.
I just edited my post, also make sure your file looke like :
20050312,RED,54343717
20050313,RED,54343717
20050316,RED,54343717
20050317,RED,54343717
20050318,RED,54343717
20050311,BLUE,54355389
20050318,BLUE,54355389
20050318,GREEN,54355555
20050320,GREEN,54355555
Note that fgetcsv($file, 128, ",") while explode the current line with a "," delimiter
and in my loop, 3 fields are required ($data[0],[1],[3])
Ok, so I'm a big fat nerd. People always tell me that I should use a different tool, than bash, for some of the scripting things that I want to do.. and I scoff.. openly.. and mockingly.. (=
So, using farmerjoe's grep examples, I created a bash script to do this.. I noticed you said something about converting it to bash or something, so here's my take on it:
Code:
#!/bin/bash
datafile='./data';
# hold which items have been found and displayed
#
used=""
# assume that no line will ever contain a space
#
for line in `cat $datafile`; do
pieces=( `echo "$line" | tr ',' ' '` ) # 0-date 1-colorname 2-value
# see if we've examined this one before
#
item="${pieces[1]},${pieces[2]}"
in_used=`echo "$used" | grep "$item"`
# if it's not yet been examined
#
if [ -z "$in_used" ]; then
# first, add it to the used list
#
used="$used $item"
count_l=`grep -c "$item" $datafile`
first_l=( `grep "$item" $datafile | head -1 | tr ',' ' '` )
last_l=( `grep "$item" $datafile | tail -1 | tr ',' ' '` )
printf "$item\t$count_l times\tfirst: $first_l\tlast: $last_l\n"
fi
done
Assuming that the file "./data" contains the data you listed above, and assuming that the file is in cronological order by date, the output should be:
Code:
~/bash> ./parse.sh
RED,54343717 5 times first: 20050312 last: 20050318
BLUE,54355389 2 times first: 20050311 last: 20050318
GREEN,54355555 2 times first: 20050318 last: 20050320
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.