Hi,
I was looking for a script to change the date from one format to other. A search in the forum gave me the below script as a result.
#! /bin/ksh
format=YYYYMMDD
YEAR=${format%????}
DAY=${format#??????}
MON=${format#$YEAR}
MON=${MON%$DAY}
echo $MON/$DAY/$YEAR
I got it from the below link
The above script works perfectly fine. But I'm really not sure how the above script works. I'm just an amateur when it comes to RegEx. A search on google did not help me either.
My question, how the above script works? Can someone trace an example for me?
Thanks
These use the shell's pattern matching operators.
YEAR=${format%????}
### Delete 4 characters from the end of format's value (format remains unchanged)
### and return that string (YYYY in this case)
DAY=${format#??????}
### Delete 6 characters from the beginning of format's value (format remains unchanged)
### and return that string (DD in this case)
MON=${format#$YEAR}
### Delete the string corresponding to YEAR's value (YYYY) from the beginning of format's value (format remains unchanged)
### and return that string (MMDD in this case)
MON=${MON%$DAY}
### Delete the string corresponding to DAY's value (DD) from the end of MON's value (MMDD)
### and return that string (MM in this case)
All this could be done using (if your shell supports it):
DAY=${format:6}
MON=${format:4:2}
YEAR=${format:0:4}
1 Like
Awesome. Thank you so much..