The unix date
command can print dates in whatever format you want to. Using the --date
flag you can print a date and time different from the current date and time. However, for the date
command to parse the date you have to remove the on in your input date. You can use the following sed command:
sed -r 's,\s+on\s+, ,g'
Combined with your desired output format, you get:
date --date="$(echo '22:06:58 on 2017-12-22' | sed -r 's,\s+on\s+, ,g')" '+%r on %B %d, %Y'
Now, you only have to combine this with your awk command, which can be done easily assuming that you always have the same format "username logged in at date". Note that you can have the on not printed from awk
at all, so the sed
call is no longer necessary:
while read line
do
echo $line | awk '{print $1" "$2" "$3" "$4" "}' | tr -d '\n'
date --date="$(echo $line | awk '{print $5" "$7}' | tr -d '.')" '+%r on %B %d, %Y'
done
For the example you gave, this prints (with locale en-NZ
):
nickname logged in at 10:06:58 PM on December 22, 2017