Calvin Calvin - 2 months ago 19
Linux Question

Format timestamp output using sed

I'm new to Linux in general, I want to change the format of my log file using sed command:

Current format:

2016-09-27file.txt::2016-09-27 18:00:00


Expected format:

2016-09-27file.txt::2016/09/27-18:00:00


How should I write the sed command?

Thanks,
Z

Answer

Following shows how to do it on a single line:

echo "2016-09-27file.txt::2016-09-27 18:00:00" | sed 's/\([0-9]*\)-\([0-9]*\)-\([0-9]*\) /\1\/\2\/\3-/'

The regexp part matches for the date part that you want to replace, which is also split-ed into sub-expression to make those part referable on replacement part.

Comments