How to Fix Time Stamps Using AWK

On Unix-based systems, you can use AWK scripts to affix time stamps to your log files. If you use "systime," the output is the seconds since the system epoch, which is usually the seconds since midnight on January 1, 1970. To fix this, use "strftime" to display a time stamp that humans can recognize. You can format your time stamps in different ways.

Instructions

    • 1

      Open your AWK script.

    • 2

      Type the following where you want a time stamp:

      `{ print strftime("%m-%d-%Y %H:%M:%S"), $0; }'

      This will display a time stamp as "12-14-2011 14:31:34." You can change the order in which the values display and use many different values. For example, if you prefer time stamps such as "December 14, 2011 02:31:34 PM," use the following command:

      `{ print strftime("%B %d,%Y %I:%M:%S %p"), $0; }'

      Note that the holder you place between the values, such as a space, comma or colon, separates the values in the time stamp. You can use many other values. Common helpful values include "%A" for the weekday, "%y" to display only the last two digits of the year and "%Z" for the time zone.

    • 3

      Save and close your work.

Tips & Warnings

  • You can quickly test your format in a Unix or Linux terminal, by typing "awk" followed by the command.

  • Time stamps are a GAWK extension for AWK, included in most distributions. If you don't have it, you will need to install it from source code.

Related Searches:

References

Comments

Related Ads

Featured