Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 字符串解析&;日期格式化unix_Bash_Datetime_Unix - Fatal编程技术网

Bash 字符串解析&;日期格式化unix

Bash 字符串解析&;日期格式化unix,bash,datetime,unix,Bash,Datetime,Unix,我正在寻找unix命令将下面的字符串解析为下面所需的格式 已连接到集成服务:[is\01]。集成服务 状态:[正在运行]集成服务启动时间:[09年5月1日] 10:27:22 2016]集成服务当前时间:[孙军05 21:57:33 2016]文件夹:[测试]工作流:[wf_MASTER_DAILY]版本[2]。工作流程 运行状态:[成功]工作流运行错误代码:[0]工作流运行 错误消息:[已成功完成。]工作流运行id[425197]。 开始时间:[2016年6月4日星期六13:14:11]结束时

我正在寻找unix命令将下面的字符串解析为下面所需的格式

已连接到集成服务:[is\01]。集成服务 状态:[正在运行]集成服务启动时间:[09年5月1日] 10:27:22 2016]集成服务当前时间:[孙军05 21:57:33 2016]文件夹:[测试]工作流:[wf_MASTER_DAILY]版本[2]。工作流程 运行状态:[成功]工作流运行错误代码:[0]工作流运行 错误消息:[已成功完成。]工作流运行id[425197]。 开始时间:[2016年6月4日星期六13:14:11]结束时间:[2016年6月4日星期六13:20:37] 2016]工作流日志文件: [/informatica/pc961/server/infa_shared/Working/infa01/WorkflowLogs/wf_MASTER_DAILY.log]

我希望解析上面的字符串并获得下面的输出(日期格式为YYYY-MM-DD HH:MM:DD)

我可以得到如下单个值的值

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
但是如何使用日期格式生成所需的输出?

如果可以使用
grep
表达式获得“开始时间”值,则可以使用下面的
date
命令将其转换为所需的时间戳(如果有
-d
选项),如:

date -d 'Sat Jun 04 13:14:11 2016' +'%Y-%m-%d %T'

下面是grep和格式化日期的最终工作代码

grep "Start time:" | cut -d'[' -f2 | cut -d']' -f1 | read dt ; date -d "$dt" +'%Y-%m-%d %T'

假设GNU 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'|' - - -

如果您正确设置输入格式,这将有所帮助。现在还不清楚这条线的起点和终点。不要使用块引号格式化日志。使用
pre
对它们进行格式化。我想我会使用您的代码,效果也很好@格伦·杰克曼
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