Find the answer to your Linux question:
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 ...
  1. #1
    Just 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

    Code:
    sort -d -o details.csv -t , -k 3,3 details.csv
    Within the csv file I only have 3 fields. A name, number and date of birth.

    Example:
    Code:
    fred,999,03/04/2005
    jane,888,01/01/1993
    james,3456,12/01/1987
    john,1234,11/08/1955
    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.

    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?

    Code:
    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	
    }
    Thanks again

  2. #2
    Just 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:

    Code:
    sort -n -o details.csv -t"/" -k3 -k1 -k2 details.csv
    Still, question 2 is still up for grabs :P

  3. #3
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    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:
    Code:
    #!/bin/bash
    
    foo=bar
    bar=hello
    
    echo "\$foo = $foo"
    echo "\$\$foo = " $(eval echo \$$foo)
    This produces the output:
    Code:
    $foo = bar
    $$foo =  hello
    We can use this ability to answer your question:
    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"
    Give this a shot! Can you figure out how it works?
    DISTRO=Arch
    Registered Linux User #388732

  4. #4
    Just 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.

  5. #5
    Just Joined!
    Join Date
    Jan 2012
    Posts
    1

    Hmmm

    CCCU by any chance?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...