0

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.

2
  • What did you try? Commented Apr 3, 2020 at 15:26
  • Does this answer your question? Convert date formats in bash Commented Apr 3, 2020 at 15:36

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.