bash - How to change the date format by storing the input in a variable in unix? -
this question has answer here:
my input
./file \[10\/04\/16 01:02:03 bst\] \[06\/08\/16 05:02:08 bst\]
i want convert \[10\/04\/16 01:02:03 bst\]
apr 10 16 01:02:03
i using following code,
echo '\[10\/04\/16 01:02:03 bst\]' | awk -f'[][/: \\\\]+' 'begin{split("jan feb mar apr may jun jul aug sep oct nov dec",m,/ /)} {print m[$3+0],$2,$4,$5":"$6":"$7}'
is possible extract result storing \[10\/04\/16 01:02:03 bst\]
in variable $starttime
, use in code? using 1 date. possible use 2 dates?
you can use date conversion script called script.sh
:
#!/bin/bash mydt() { ifs='/' read -ra arr <<< "${1//[\[\]\\]}" tz=':europe/london' date -d "${arr[1]}/${arr[0]}/${arr[2]}" '+%b %d %y %t' } var1="$(mydt "$1")" var2="$(mydt "$2")" echo "$var1" echo "$var2"
then call as:
bash script.sh '\[10\/04\/16 01:02:03 bst\]' '\[06\/08\/16 05:02:08 bst\]'
output:
apr 10 16 01:02:03 aug 06 16 05:02:08
Comments
Post a Comment