Skip to main content
That is GNU specific.
Source Link
Stéphane Chazelas
  • 586.4k
  • 96
  • 1.1k
  • 1.7k

This first uses sed to convert the format you gave, 10152015, to the format mm/dd/yyyy, which the GNU implementation of the date command, with its -d options understands. This is accomplished by echoing the original date into sed which inserts the slashes /. This re-formatted date value is stored in D. Next we call datedate with instructions to print the output format "mmddyyyy", and tell it to print the date of yesterday using $D as the current date.

#!/bin/sh D=$(echo $"${1-10152015}" | sed 's,^\(..\)\(..\)\(....\)$,\1/\2/\3,') date +%m%d%Y -d"$D yesterday" 

This first uses sed to convert the format you gave, 10152015, to the format mm/dd/yyyy, which the date command understands. This is accomplished by echoing the original date into sed which inserts the slashes /. This re-formatted date value is stored in D. Next we call date with instructions to print the output format "mmddyyyy", and tell it to print the date of yesterday using $D as the current date.

#!/bin/sh D=$(echo ${1-10152015} | sed 's,^\(..\)\(..\)\(....\)$,\1/\2/\3,') date +%m%d%Y -d"$D yesterday" 

This first uses sed to convert the format you gave, 10152015, to the format mm/dd/yyyy, which the GNU implementation of the date command, with its -d options understands. This is accomplished by echoing the original date into sed which inserts the slashes /. This re-formatted date value is stored in D. Next we call date with instructions to print the output format "mmddyyyy", and tell it to print the date of yesterday using $D as the current date.

#!/bin/sh D=$(echo "${1-10152015}" | sed 's,^\(..\)\(..\)\(....\)$,\1/\2/\3,') date +%m%d%Y -d"$D yesterday" 
cut/paste typo correction
Source Link
RobertL
  • 6.9k
  • 1
  • 22
  • 39

This first uses sed to convert the format you gave, 10152015, to the format mm/dd/yyyy, which the date command understands. This is accomplished by echoing the original date into sed which inserts the slashes /. This re-formatted date value is stored in D. Next we call date with instructions to print the output format "mmddyyyy", and tell it to print the date of yesterday using $D as the current date.

date +%m%d%Y -d"$#!/bin/sh D=$(echo ${1-10152015} | sed 's,^\(..\)\(..\)\(....\)$,\1/\2/\3,') date +%m%d%Y -d"$D yesterday" 
date +%m%d%Y -d"$(echo 10152015 | sed 's,^\(..\)\(..\)\(....\)$,\1/\2/\3,') yesterday" 

This first uses sed to convert the format you gave, 10152015, to the format mm/dd/yyyy, which the date command understands. This is accomplished by echoing the original date into sed which inserts the slashes /. This re-formatted date value is stored in D. Next we call date with instructions to print the output format "mmddyyyy", and tell it to print the date of yesterday using $D as the current date.

#!/bin/sh D=$(echo ${1-10152015} | sed 's,^\(..\)\(..\)\(....\)$,\1/\2/\3,') date +%m%d%Y -d"$D yesterday" 
Source Link
RobertL
  • 6.9k
  • 1
  • 22
  • 39

date +%m%d%Y -d"$(echo 10152015 | sed 's,^\(..\)\(..\)\(....\)$,\1/\2/\3,') yesterday"