使用linux bash shell格式化文件中的日期字符串 [英] Formatting date strings in a file with linux bash shell

查看:904
本文介绍了使用linux bash shell格式化文件中的日期字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我整理文件时,输出格式的示例是:

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

我希望格式为

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

我需要使用Linux bash shell来执行此操作.如果有人可以帮助我,我将非常感激.

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

推荐答案

当性能至关重要时.把它放在script.awk:

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
}

用法:awk -f script.awk logfile

输出:


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

这篇关于使用linux bash shell格式化文件中的日期字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆