-2

I am creating the logs and pushing to logstash. The date format of the logstash server is of the below destination form. In the Linux server, I can use the date command to convert the local date/time to log file. How to use the date command to get the below format in the Linux server. What is the format of the below command? I can set the format as TZ='CEST' date +"%field". I tried to set few format specifies but could not get the string 'th' in the date of the month. Also I don't understand the last field '221' in the date. Any help ?

destination server format: August 4th 2017, 22:30:45.221

intechops6
  • 1,007
  • 4
  • 22
  • 43
  • 1
    The last field contains fractional seconds (that is, the last field is `45.221` seconds). Are you *sure* that's the only format supported by the logstash server? That would be unusual. – larsks Aug 04 '17 at 18:05
  • 1
    https://stackoverflow.com/questions/2495459/formatting-the-date-in-unix-to-include-suffix-on-day-st-nd-rd-and-th –  Aug 04 '17 at 18:45

1 Answers1

1

try this code:

#!/bin/sh
DaySuffix() {
  case `date +%d` in
    1|21|31) echo "st";;
    2|22)    echo "nd";;
    3|23)    echo "rd";;
    *)       echo "th";;
  esac
}
date "+%B %-d`DaySuffix` %Y, %T.%3N"

result for now: August 4th 2017, 12:04:49.750

help source: Formatting the date in unix to include suffix on day (st, nd, rd and th)

Ikrom
  • 474
  • 4
  • 10
  • IKrom @ This is a smart approach to format to get the user-defined date format. @jww - I am using the date command in the bash script to feed the date to command output in bash script. So posted here. – intechops6 Aug 07 '17 at 11:42
  • @arunp Set please also the Green flag for the answer if its helped you. Thanks ). – Ikrom Aug 07 '17 at 16:14