2

i have a string containing date and time as timestamp= 12-12-2012 16:45:00

I need to reformat it into timestamp= 16:45:00 12-12-2012

How to achieve this in shell script?

Note Please : variable's value is 12-12-2012 16:45:00 and timestamp is the name of variable

#!usr/bin/expect
set timestamp "16:45:00 12-12-2012"
Now what should i do so value of timestamp will become 12-12-2012 16:45:00
script extention is .tcl example test.tcl
Astro - Amit
  • 767
  • 3
  • 15
  • 36

4 Answers4

4

You could use variable patterned removal. ## means "greedily remove everything that matches the pattern, starting from the left". %% means the same from the right:

tm=${timestamp##* }
dt=${timestamp%% *}
result="$tm $dt"

or you could use cut to do the same, giving a field delimiter:

tm=$(echo $timestamp | cut -f2 -d' ')
dt=$(echo $timestamp | cut -f1 -d' ')
result="$tm $dt"

or you could use sed to swap them with a regex (see other post).

or if you are pulling the date from the date command, you could ask it to format it for you:

result=$(date +'%r %F')

and for that matter, you might have a version of date that will parse your date and then let you express it however you want:

result=$(date -d '12/12/2012 4:45 pm' +'%r %F')

admittedely, this last one is picky about date input...see "info date" for information on accepted inputs.

If you want to use regex, I like Perl's...they are cleaner to write:

echo $timestamp | perl -p -e 's/^(\S+)\s+(\S+)/$2 $1/'

where \S matches non-space characters, + means "one or more", and \s matches spaces. The parens do captures of the parts matched.

EDIT:

Sorry, didn't realize that the "timestamp=" was part of the actual data. All of the above example work if you first strip that bit out:

var='timestamp=2012-12-12 16:45:11'
timestamp=${var#timestamp=}
... then as above ...
Tony K.
  • 5,535
  • 23
  • 27
2

Using sed:

sed 's/\([0-9]*-[0-9]*-[0-9]*\)\([ \t]*\)\(.*\)/\3\2\1/' input

this command works on lines containing the pattern number-number-number whitespace antyhing. It simply swaps the number-number-number part \([0-9]*-[0-9]*-[0-9]*\) with the anything part \(.*\), also keeping the original whitespaces \([ \t]*\). So the replace part of sed is \3\2\1, which means the third part, white spaces, and the first part.

Same logic with tcl:

set timestamp "12-12-2012 16:45:00"
set s [regsub {([0-9]*-[0-9]*-[0-9]*)([ \t]*)(.*)} $timestamp \\3\\2\\1]
puts $s
perreal
  • 94,503
  • 21
  • 155
  • 181
  • :will this work in tcl script also ? and i need to use above as : **sed 's/\([0-9]*-[0-9]*-[0-9]*\)\([ \t]*\)\(.*\)/\3\2\1/' timestamp** can you tell me how this **sed** command is working here? – Astro - Amit Dec 21 '12 at 02:26
  • 1
    The parens grab the part that matches...they count from left to right. So, the first thing grabbed is in \1, next in \2, etc. The / delimits the pattern to match from the replacement string. – Tony K. Dec 21 '12 at 02:34
  • @perreal : it's not changes its printing the same value as before – Astro - Amit Dec 21 '12 at 04:58
  • @perreal yes it works but please check the orignal question **date /time** has to be changed in to **time/date** you just did reverse, and can you explain the working also :) **2012-12-12 20:20:20** to **20:20:20 2012-12-12** – Astro - Amit Dec 21 '12 at 05:25
  • 1
    Same logic - pick the bits to swap with a regular expression and swap them as strings - but in two languages. Note that the Tcl example uses the wrong input string, and writes it to a different variable, but works on the right one and can be witten back to `timestamp` just as easily. – Donal Fellows Dec 21 '12 at 05:59
1

solution here:

string="timestamp= 12-12-2012 16:45:00"
awk '{print $1, $3, $2}' <<< "$string"
sampson-chen
  • 45,805
  • 12
  • 84
  • 81
0

In bash (and similar shells):

$ timestamp="12-12-2012 16:45:00"
$ read -a tsarr <<< "$timestamp"
$ echo "${tsarr[1]} ${tsarr[0]}"
16:45:00 12-12-2012
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358