Results 1 to 5 of 5
Hello again.
I'm currently using the following technique to sort a file called details.csv
Code:
sort -d -o details.csv -t , -k 3,3 details.csv
Within the csv file I only ...
- 12-13-2011 #1Just Joined!
- Join Date
- Dec 2011
- Posts
- 5
[BASH] Sorting by date and creating generic functions
Hello again.
I'm currently using the following technique to sort a file called details.csv
Within the csv file I only have 3 fields. A name, number and date of birth.Code:sort -d -o details.csv -t , -k 3,3 details.csv
Example:
When I call the above sort, it is sorting the third field but it's doing so by the day (the format is dd/mm/yyyy). I'd love it to sort correctly by the entire date, or if it's too much hassle I'm happy to just sort by year.Code:fred,999,03/04/2005 jane,888,01/01/1993 james,3456,12/01/1987 john,1234,11/08/1955
I've tried playing around with the sort function, but this is the best way I can come up with without breaking everything. Hopefully one of you knowledgeable chaps can point me in the right direction.
Secondly, I have 2 functions that do the same job, but with different variables. Is there a way to just use one of these functions and return the "chopped" variable?
Thanks againCode:chop_name() { length=`echo ${#name}` if [ $length -gt 6 ] then name=`echo ${name:0:6}` name=$name".." cut="true" fi } chop_number() { length=`echo ${#number}` if [ $length -gt 6 ] then number=`echo ${number:0:6}` number=$number".." #place dots at the end to show number's been chopped cut="true" fi }
- 12-13-2011 #2Just Joined!
- Join Date
- Dec 2011
- Posts
- 5
Bah.
I realised I was making it difficult for myself by having the date of birth LAST in the csv. I made it the first and used this sort:
Still, question 2 is still up for grabs :PCode:sort -n -o details.csv -t"/" -k3 -k1 -k2 details.csv
- 12-14-2011 #3
Did you think that Linux would let you down? Of course you can!

In this case, we are going to use a feature called indirect references. This allows you to use a variable to store another variable's name.
Let's see an example:
This produces the output:Code:#!/bin/bash foo=bar bar=hello echo "\$foo = $foo" echo "\$\$foo = " $(eval echo \$$foo)
We can use this ability to answer your question:Code:$foo = bar $$foo = hello
Give this a shot! Can you figure out how it works?Code:#!/bin/bash chop_variable() { var_name=$1 length=$(eval echo \${#$var_name}) if [ "$length" -gt 6 ]; then new_value=$(eval echo \${$var_name:0:6}) new_value=${new_value}.. fi echo $new_value } chop_name() { name=$(chop_variable name) } chop_number() { number=$(chop_variable number) } name=hellotherefriend number=1234567890 chop_name chop_number echo "name = $name" echo "number = $number"DISTRO=Arch
Registered Linux User #388732
- 12-14-2011 #4Just Joined!
- Join Date
- Dec 2011
- Posts
- 5
Thanks very much Cabhan, that is ideal. I appreciate the explanation as well, it helps greatly!
I've only been playing with Bash on and off for a couple of weeks, but I have found it to be amazingly intuitive. There are a couple of odd niggles, like the fact functions only return a "state" rather than a value. Other than that, I've really enjoyed my time using bash.
- 01-11-2012 #5Just Joined!
- Join Date
- Jan 2012
- Posts
- 1
Hmmm
CCCU by any chance?


Reply With Quote