1

When I cat the file an example of the output format is:

ok: servername Mon May 23 00:00:00 EDT 2018
ok: servername Thu Jul 16 00:00:00 EDT 2019

I would like the format to be something like

ok: servername 05/23/2018
ok: servername 07/16/2019

I need to use the Linux bash shell to do it. If any one could help me I be very grateful.

Cyrus
  • 84,225
  • 14
  • 89
  • 153
Joshua
  • 11
  • 1

3 Answers3

3

When performance matters. Put this in script.awk:

BEGIN{ 
  m["Jan"]="01"; m["Feb"]="02"; m["Mar"]="03"; m["Apr"]="04"; 
  m["May"]="05"; m["Jun"]="06"; m["Jul"]="07" # to be completed
}
{
  print $1, $2, m[$4] "/" $5 "/" $8
}

Usage: awk -f script.awk logfile

Output:

ok: servername 05/23/2018
ok: servername 07/16/2019
Cyrus
  • 84,225
  • 14
  • 89
  • 153
2

With GNU date, you can specify an input file containing all your date strings; combined with cut and paste:

paste -d ' ' \
    <(cut -d ' ' -f-2 infile) \
    <(date -f <(cut -d ' ' -f3- infile) '+%m/%d/%Y')

Output:

ok: servername 05/23/2018
ok: servername 07/16/2019

This uses process substitution to build a temporary input file for date -f, and for paste to build a new output file.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
0

According man date, the command is able to

display time described by STRING, not 'now'

via --date or -d. So if you store your values in a variable

DATE="Thu Jul 16 00:00:00 EDT 2019"

you could use something like

date --date="${DATE}" +%m/%d/%Y

to reformat the timestamp.

For more information you may have a look into Convert date formats in bash.

U880D
  • 8,601
  • 6
  • 24
  • 40