Let's say i have a line in the format of "YYYY-MM-DD-HH:MM:SS", how do i change it to "DD-MM-YYY HH:MM" ?
I've only managed to come up with this : awk -F'-' '{ YY=$1 ; MM=$2 ; DD=$3 ; print DD MM YY }'
which would output DDMMYYY .
CodePudding user response:
plain bash:
input="YYYY-MM-DD HH:MM:SS"
IFS=": -" read y m1 d h m2 s <<<"$input"
output="$d-$m1-$y $h:$m2"
calling awk from shell:
input="YYYY-MM-DD HH:MM:SS"
output=(
echo "$input" | awk '
split($0,a,/[: -]/) {
printf "%s-%s-%s %s:%s\n", a[3],a[2],a[1],a[4],a[5]
}
'
)
Both assume input is well-formed.
CodePudding user response:
Assuming you want YYYY
:
input="YYYY-MM-DD-HH:MM:SS"
awk -F '[:-]' '{print $3 "-" $2 "-" $1 " " $4 ":" $5}' <<<"$input"
output
DD-MM-YYYY HH:MM
CodePudding user response:
If you want to convert a real date
$ input="2022-06-01-11:10:00"
$ date -d "${input%-*} ${input##*-}" '%d-%m-%Y %H:%M'
01-06-2022 11:10
GNU awk
awk '
BEGIN{ format = "%d-%m-%Y %H:%M" }
{
gsub(/[-:]/," ");
print strftime(format, mktime($0))}
' <<<"$input"
01-06-2022 11:10
$ cat file
2022-06-01-11:10:01
2022-07-02-12:11:02
2022-08-03-13:12:03
$ awk 'BEGIN{ format = "%d-%m-%Y %H:%M" }{gsub(/[-:]/," ");print strftime(format, mktime($0))}' file
01-06-2022 11:10
02-07-2022 12:11
03-08-2022 13:12