I need to combine this sed command:
sed 's/T[[:digit:]].*//' song.json
with this other sed command:
sed -r 's/([[:digit:]]{2,4})([[:digit:]]{1,2})([[:digit:]]{1,2})/\3\-\2\-\1/g'
What I intend to do is transform this date format:
20200426T200000
in this other:
26-04-2020
How can I combine the two sed commands?
Just put a semicolon:
Example where I substitute 2 for X and 3 for Y:
In your case:
That said, in your particular case you can directly use
date
to write the date in a different way.20200426T200000
and are only interested in the part to the left of the "T".date -d"<esa fecha>" "<un formato>"
it solves the problem.All together:
With your value:
Combining comes to mind like this:
What happens here is very simple and it
sed
has the parameters:-e
Indicates that it adds a script (your commands/etc//
),-r
Enable regular expressions,so
sed -e '<script>' -re '<script con regex no "triviales">'
, then with-e
you can combineNote : That is very "ugly". If someone (including you) wanted to edit it in the future, it wouldn't be as smooth as it could be.
My proposal, which includes itself
sed
, is this:Which uses a regular expression where I only capture in a group first four digits, then two, then two; this to then catch what follows (which doesn't interest me) and rearrange it as you wanted.
It would be useful for you to see if all the dates received are going to follow that format (YYYYMMDDTHHMMSS) so that there are no problems with the format. It is very common to find problems with the date format.
Another option that I offer you is the use of
awk
:where I use the function
substr
to indicate the index of where I want it to search on the record and the number of characters it should take from there.