Databases
cancel
Showing results for 
Search instead for 
Did you mean: 

How to cut the last 2 characters of a date parameter

SOLVED
Go to solution
Suman_7
Frequent Advisor

How to cut the last 2 characters of a date parameter

I have a parameter which has a value
parm_date=20040401
I need to check if the day is first day of a month

how do I grep the '01' portion of this date?

Thanks
5 REPLIES
A. Clay Stephenson
Acclaimed Contributor

Re: How to cut the last 2 characters of a date parameter

Since your date string has no spaces, let's do this in an unconventional way. You can simply do a MOD 100.

DT=20040401
typeset -i10 DAY=$(( ${DT} % 100 ))
if [[ ${DAY} -eq 1 ]]
then
echo "First Day of Month"
fi
If it ain't broke, I can fix that.
Sanjay Kumar Suri
Honored Contributor
Solution

Re: How to cut the last 2 characters of a date parameter

Check the following:
parm_date=20040401
dd=`expr substr $parm_date 7 2`
echo $dd

sks
A rigid mind is very sure, but often wrong. A flexible mind is generally unsure, but often right.
Victor Fridyev
Honored Contributor

Re: How to cut the last 2 characters of a date parameter

Try this:


if grep ......01$ >/dev/null ; then
echo this is a first day of a month
else
:
fi
Entities are not to be multiplied beyond necessity - RTFM
Bill Hassell
Honored Contributor

Re: How to cut the last 2 characters of a date parameter

How about something simple like cut/if rather than grep:

parm_date=20040401
DAY=$(echo $parm_date | cut -c 7-8)
if [ "$DAY" = "01" ]
then
...
fi


Bill Hassell, sysadmin
Arturo Galbiati
Esteemed Contributor

Re: How to cut the last 2 characters of a date parameter

in_date=20040401
dd=$(echo $in_date | cut -c 7-8)
if [ "$dd" = "01" ]
then
...
fi

HTH
Art