PRK
PRK

Reputation: 431

How can I print numbers as HH MM SS MM DD YYYY?

ls -ltr
-rw-rw-r-- 1 soper sbcprd 36646 Jul 29 21:07 END_TIME_20150729_203759.log
-rw-rw-r-- 1 soper sbcprd 36647 Jul 30 20:37 END_TIME_20150730_201717.log
-rw-rw-r-- 1 soper sbcprd 36643 Jul 31 21:00 END_TIME_20150731_202028.log
-rw-rw-r-- 1 soper sbcprd 36597 Aug  1 16:32 END_TIME_20150801_162901.log
-rw-rw-r-- 1 soper sbcprd 36658 Aug  3 20:53 END_TIME_20150803_202725.log
-rw-rw-r-- 1 soper sbcprd 36645 Aug  4 20:51 END_TIME_20150804_202831.log
-rw-rw-r-- 1 soper sbcprd 36798 Aug  5 20:37 END_TIME_20150805_202013.log

Last column shows the ending time as YYYYMMDD_HHMMSS.

How can I print these numbers as HH MM SS MM DD YYYY?

Upvotes: 0

Views: 90

Answers (2)

John1024
John1024

Reputation: 113864

The OP appears to be asking for a reformatting of the the times specified in the file's names. Because those times differ from the file's time stamp, we cannot get the numbers we want by manipulating ls options.

Using sed:

$ ls -ltr | sed -r 's/.*([[:digit:]]{4})([[:digit:]]{2})([[:digit:]]{2})_([[:digit:]]{2})([[:digit:]]{2})([[:digit:]]{2}).log$/\4 \5 \6 \2 \3 \1/'
20 37 59 07 29 2015
20 17 17 07 30 2015
20 20 28 07 31 2015
16 29 01 08 01 2015
20 27 25 08 03 2015
20 28 31 08 04 2015
20 20 13 08 05 2015

Or, if we want to keep the file's name, just reformatting the numbers in the name:

$ ls -ltr | sed -r 's/([[:digit:]]{4})([[:digit:]]{2})([[:digit:]]{2})_([[:digit:]]{2})([[:digit:]]{2})([[:digit:]]{2})/\4 \5 \6 \2 \3 \1/'
-rw-rw-r-- 1 soper sbcprd 36646 Jul 29 21:07 END_TIME_20 37 59 07 29 2015.log
-rw-rw-r-- 1 soper sbcprd 36647 Jul 30 20:37 END_TIME_20 17 17 07 30 2015.log
-rw-rw-r-- 1 soper sbcprd 36643 Jul 31 21:00 END_TIME_20 20 28 07 31 2015.log
-rw-rw-r-- 1 soper sbcprd 36597 Aug 1 16:32 END_TIME_16 29 01 08 01 2015.log
-rw-rw-r-- 1 soper sbcprd 36658 Aug 3 20:53 END_TIME_20 27 25 08 03 2015.log
-rw-rw-r-- 1 soper sbcprd 36645 Aug 4 20:51 END_TIME_20 28 31 08 04 2015.log
-rw-rw-r-- 1 soper sbcprd 36798 Aug 5 20:37 END_TIME_20 20 13 08 05 2015.log

How it works

We use a single sed command which is substitution. The substitution command looks like s/old/new/ where old is a regular expression and new can refer back to groups that we matched in old.

Let's look at old:

  • .* matches anything, starting, in this case with the beginning of the line.

  • ([[:digit:]]{4}) matches four digits and saves them in group 1, denoted \1. This is the year

  • ([[:digit:]]{2}) matches two digits and saves them in group 2, denoted \2. This is the month

  • ([[:digit:]]{2}) matches two digits and saves them in group 3, denoted \3. This is the day.

  • _ matches the underline character.

  • ([[:digit:]]{2}) matches two digits and saves them in group 4, denoted \4. This is the hour

  • ([[:digit:]]{2}) matches two digits and saves them in group 5, denoted \5. This is the minutes.

  • ([[:digit:]]{2}) matches two digits and saves them in group 6, denoted \6. This is the seconds.

  • .log$ matches the .log at the end of the file name.

Everything that matched above is replaced with what is in new which, in our case, consists of:

  • \4 \5 \6 \2 \3 \1

    These are the six groups that we matched above but reordered.

Upvotes: 3

Andrzej Pronobis
Andrzej Pronobis

Reputation: 36096

Just pipe it through the following awk program:

awk '{print substr($9,19,2) " " substr($9,21,2) " " substr($9,23,2) " " substr($9,14,2) " " substr($9,16,2) " " substr($9,10,4)}'

which should give:

20 37 59 07 29 2015
20 17 17 07 30 2015
20 20 28 07 31 2015
16 29 01 08 01 2015
20 27 25 08 03 2015
20 28 31 08 04 2015
20 20 13 08 05 2015

Upvotes: 1

Related Questions