Guys, I have a variable in a script that I want to transform to into something else Im hoping you guys can help. It doesn't have to use sed/awk but I figured these would be the simplest.
DATE=20160120
I'd like to transform $DATE into "01-20-16" and move it into a new variable called $SIMPLEDATE
the format is always the same, notice I need to transform 2016 in just 16 and move it around.
This is perfect man, thank you...
Can you explain exactly whats going on here? I read some info on sed but to be honest Im not following. I'd love to learn so I can do this myself next time.
The sed code starts with s , the substitute command.
The following character / is a delimiter, followed by a Regular Expresson, a delimiter, and the substitution string. The substitution string is terminated by another delimiter (that can be followed by an option).
In a Regular Expression each . matches a character.
By a \( \) one can mark groups.
The first group contains the 3rd and 4th character, the second group contains the 5th and 6th character, ...
The reference to the first group is \1
The second group should be printed first, so the \2 is first in the substitution string. Followed by a - , followed by the third group, ...