我正在寻找unix命令来将以下字符串解析为所需的格式,如下所示。
连接到Integration : is_infa01。集成服务状态:运行集成服务启动时间:09年5月10日:27:22 2016集成服务当前时间:孙俊05 21:57:33 2016文件夹:测试工作流: wf_MASTER_DAILY版本2。工作流运行状态:成功工作流运行错误代码:工作流运行错误消息:成功完成。工作流运行id 425197。启动时间:星期六6月04 13:14:11 2016年结束时间:星期六6月04 :20:37 2016工作流日志文件: /informatica/pc961/server/infa_shared/Working/infa01/WorkflowLogs/wf_MASTER_DAILY.log
我希望解析上面的字符串并得到下面的输出(日期格式为YYYY DD:MM:DD)
Workflow run status|Start time|End time
Succeeded|2016-06-04 13:14:11|2016-06-04 13:20:37我可以得到单个值的值,如下所示
grep "Workflow run status:" | cut -d'[' -f2 | cut -d']' -f1
grep "Start time:" | cut -d'[' -f2 | cut -d']' -f1
grep "End time:" | cut -d'[' -f2 | cut -d']' -f1但是,如何使用日期格式生成所需的输出?
发布于 2016-06-06 14:55:41
假设grep,您可以像这样把它包装起来:
string="Connected to Integration Service: [is_infa01]. Integration Service status: [Running] Integration Service startup time: [Mon May 09 10:27:22 2016] Integration Service current time: [Sun Jun 05 21:57:33 2016] Folder: [TEST] Workflow: [wf_MASTER_DAILY] version [2]. Workflow run status: [Succeeded] Workflow run error code: [0] Workflow run error message: [Completed successfully.] Workflow run id [425197]. Start time: [Sat Jun 04 13:14:11 2016] End time: [Sat Jun 04 13:20:37 2016] Workflow log file: [/informatica/pc961/server/infa_shared/Working/infa01/WorkflowLogs/wf_MASTER_DAILY.log]"
w="Workflow run status" s="Start time" e="End time"
{
printf "%s\n" "$w" "$s" "$e"
grep -oP "$w: \\[\\K.*?(?=\\])" <<<"$string"
date -d "$(grep -oP "$s: \\[\\K.*?(?=\\])" <<<"$string")" "+%F %T"
date -d "$(grep -oP "$e: \\[\\K.*?(?=\\])" <<<"$string")" "+%F %T"
} | paste -d'|' - - -输出
Workflow run status|Start time|End time
Succeeded|2016-06-04 13:14:11|2016-06-04 13:20:37发布于 2016-06-06 05:19:35
如果可以使用grep表达式获得“开始时间”值,则可以使用下面的date命令将其转换为所需的时间戳,如果您有-d选项,如:
date -d 'Sat Jun 04 13:14:11 2016' +'%Y-%m-%d %T'发布于 2016-06-06 14:40:06
以下是grep的最终工作代码&格式化日期
grep "Start time:" | cut -d'[' -f2 | cut -d']' -f1 | read dt ; date -d "$dt" +'%Y-%m-%d %T'https://stackoverflow.com/questions/37649909
复制相似问题