<---- template headericclude ----->
sed question(switching characters)?
FedoraForum.org - Fedora Support Forums and Community
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2005
    Location
    Wisconsin
    Age
    44
    Posts
    241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    sed question(switching characters)?

    Maybe not exactly programming but does anyone know how with sed to change a date like 03/20/2006 to 2006-03-20?
    Thanks,

    Chris.

  2. #2
    Join Date
    Feb 2005
    Location
    Scotland
    Posts
    445
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I can't rememer how to use sed much so here's a bash script especially for you

    Save the following in a file called dateConverter.sh (the web site won't let me upload files of this type). Do a "chmod u+x dateConverter.sh". Then run it with your date as an a parameter.

    Code:
    #Converts dates from mm/dd/yyyy to yyyy-mm-dd
    #by Shakes
    
    if [ $# -eq 0 ]; then
       echo "Usage: dateConvert.sh mm/dd/yyyy"
    	exit 1
    fi
    
    IFS="/"
    index=1
    
    for x in ${1}
    do
       datePart[$index]=$x
       let index=$index+1
    done
    
    echo ${datePart[3]}-${datePart[1]}-${datePart[2]}
    I could clearly make this cleverer and start let u speify abitrary formats and things but hopefully it'll match ur needs.

  3. #3
    Jman Guest
    date command makes this easy:
    Code:
    date -d "03/20/2006" +%F
    See the man page and date --help for more info.

  4. #4
    Join Date
    Feb 2005
    Location
    Scotland
    Posts
    445
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    damn I didn't realise that the date command could take date as input. Still nice to do some bash again :-)

  5. #5
    Join Date
    Jun 2005
    Location
    Wisconsin
    Age
    44
    Posts
    241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,
    Thanks everyone. I actually decided to do it with awk. Pretty easy that way.

    Code:
    function chgDate(STRING){
           newDate=substr(STRING,7,4) "-" substr(STRING,1,2) "-" substr(STRING,4,2)
           return newDate
    }
    {
       if (NR > 1) {
          printf "insert into tbl_runTime1 values(%s,'%s','%s','%s','%s','%s');\n", $1,$2,$3,$4,chgDate($5),$6
       }
    }
    Thanks,

    Chris.

Similar Threads

  1. Block Characters in GUI
    By grgaud in forum Servers & Networking
    Replies: 2
    Last Post: 10th April 2007, 04:48 PM
  2. repeating characters
    By Robgould in forum Using Fedora
    Replies: 3
    Last Post: 30th March 2006, 08:29 PM
  3. characters in file
    By LackeyLad in forum Programming & Packaging
    Replies: 1
    Last Post: 17th December 2004, 04:51 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
[[template footer(Guest)]]