Eliminating time zone from date string in a Unix shell script - bash

I have date formatted like below and I want to remove GMT+05:30 from the date using a shell script.
Tue Dec 4 17:30:51 GMT+05:30 2012
and output should be
Tue Dec 4 17:30:51 2012
I am new to shell script and still learning.

Use sed if you can't change the date output:
... | sed 's/GMT+5:30 //g' | ...
But a better solution is to use date formatting capabilities:
date +"%a %b %d %T %Y"
(for details, see man date)

Related

bash substring of command instead of variable?

I can do this in bash:
foo=bar
echo ${foo:0:2}
which prints 'ba' (the first two characters of 'bar').
Now I want to do the same with a script/command output instead of a variable, like so:
echo ${$(date):0:10}
But then I get an error: "bad substitution".
Of course I can use an intermediary variable:
foo=$(date)
echo ${foo:0:10}
But is there a way to do this directly?
P.S. The date command is just an example, this is not about generating some date string in a particular format. Just the general concept of taking a substring from an arbitrary shell command output.
No, BASH syntax doesn't allow any kind of nesting. You can do so using external utilities like cut:
date | cut -c 1-10
Wed Jun 13
To replace date:
$ date
Wed Jun 13 14:57:38 EEST 2018
you can use printf:
$ printf "%(%a %b %d%n)T"
Wed Jun 13
man strftime for more format modifiers.
If you want the output Wed Jun 13 for today's date (which this is), then
$ date +'%a %b %e'
Wed Jun 13
See the manual for date and/or for strftime on your system.

`Date` command formatting in Solaris Unix?

How to the below output using date formatting..!?
Date command output should look like:
Mon Oct 24 12:01:32 SGT 2016:
date +%a" "%b" "%d" "%T" "%Z" "%Y
This will provide the expected output..!

How to convert local date-time string to Unix timestamp (GMT)?

time_var="6/23/2016 3:20:00 AM"
(this is in EDT)
We need to get unix timestamp for this variable after converting its value to GMT.
Just use the -u flag while passing the date with -d:
$ time_var="6/23/2016 3:20:00 AM"
$ date -d"$time_var EDT" -u
Thu Jun 23 07:20:00 UTC 2016
Note I also appended EDT to your date.
From man date:
-d, --date=STRING
display time described by STRING, not 'now'
-u, --utc, --universal
print or set Coordinated Universal Time

Find and Echo only the date (with format) in String Output on Bash

I am trying to get the date "+%a %b %d %R:%S %Y" in bash.
here's the sample command and output
$ xscreensaver-command --time
XScreenSaver 5.32: screen non-blanked since Thu Oct 29 12:15:05 2015 (hacks: #184, #60)
I am trying to get the the value Thu Oct 29 12:15:05 2015 on the string.
How can I achieve this?
Try to append with GNU grep:
2>&1 | grep -Po 'since \K.*(?= \()'
Output:
Thu Oct 29 12:15:05 2015

"date -d" doesn't accept ISO 8601 format

Am I doing something wrong? I can't believe date -d does not accept its own output when I use the ISO 8601 option with seconds. I either have to remove the 'T' or the timezone to get it to work.
> date -Iseconds
2014-01-14T11:07:57-0800
> date -d "2014-01-14T11:07:57-0800"
date: invalid date `2014-01-14T11:07:57-0800'
> date -d "2014-01-14 11:07:57-0800" # remove the 'T'
Tue Jan 14 11:07:57 PST 2014
> date -d "2014-01-14T11:07:57" # remove the timezone
Mon Jan 13 20:07:57 PST 2014
My current solution is to use date +"%Y-%m-%d %T %z" instead of date -Iseconds but I just thought that was strange.
Perhaps your locale is interfering with the date parsing?
Try specifying the time locale:
$ LC_TIME=C date -d "2014-01-14T11:07:57-0800"
Tue Jan 14 11:07:57 PST 2014
This works for me with GNU date 8.13.
GNU coreutils have only supported ISO 8601 dates as input since version 8.13 (released on 2011-09-08).
Thanks to #John1024 for the reference to
https://unix.stackexchange.com/questions/107750/how-to-parse-iso8601-dates-with-linux-date-command

Resources