`Date` command formatting in Solaris Unix? - shell

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..!

Related

How I can convert date to following format in Shell?

I am passing a date as an argument and needed to convert it in given format in shell.
Pasting the sample code here
echo "20190101120001" | xargs date +"%Y%m%d%H%M%S" -u
And this should display output as
Tue Jan 01 12:00:01 UTC 2019
try this:
#!/bin/bash
str="20190101120001"
date -d "${str:0:8} ${str:8:2}:${str:10:2}:${str:12:2}" -u

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.

Convert date String to number on Solaris shell script gives No such file or directory

I have a date in the format "Thu Sep 22 3:50 2016", and I want to convert it to format: "2016-09-22"
I tried the following shell script, which works fine for 'date', but gives error for user specified string: (I am working on Solaris platform). Any inputs will be helpful.
Input:
#!/usr/bin/sh
mydate="Thu Sep 22 3:50 2016"
echo `date +"%Y-%m-%d"`
echo `$mydate +"%Y-%m-%d"`
Output
./testShell.sh
**2016-09-22**
./testShell.sh[6]: Thu: not found **[No such file or directory]**
Any pointers please?
Under Solaris 11, many GNU utilities are available under the /usr/gnu/bin directory so you just need to slightly modify your script to either use the full path the the GNU variant :
#!/bin/sh
mydate="Thu Sep 22 3:50 2016"
date +"%Y-%m-%d"
/usr/gnu/bin/date -d "$mydate" +"%Y-%m-%d"
or use the already existing symlink prefixed by g (for GNU):
gdate -d "$mydate" +"%Y-%m-%d"
or set your PATH to look at /usr/gnu/bin first and keep your script unchanged.
PATH=/usr/gnu/bin:$PATH
You can try something like this;
#!/bin/bash
mydate="Thu Sep 22 3:50 2016"
date +"%Y-%m-%d"
date -d "$mydate" "+%Y-%m-%d"

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

Eliminating time zone from date string in a Unix shell script

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)

Resources