2

I am looking for unix command to parse the below strings to the desired format as below.

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]

I am looking to parse the above string and get the below output (with date format as YYYY-MM-DD HH:MM:DD)

Workflow run status|Start time|End time Succeeded|2016-06-04 13:14:11|2016-06-04 13:20:37 

I can get the value of individual values like below

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 

but how to make the desired output with date formatting?

1
  • It would help if you format the input properly. It's unclear where the line starts & where it ends. Do not format logs using block-quote. Use pre for formatting them. Commented Jun 6, 2016 at 5:48

3 Answers 3

2

If you can get the "start time" value using your grep expression, you can use below date command to convert it into desired timestamp if you have -d option, like :

date -d 'Sat Jun 04 13:14:11 2016' +'%Y-%m-%d %T' 
Sign up to request clarification or add additional context in comments.

Comments

0

Assuming GNU grep, you can wrap it all up like this:

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'|' - - - 

outputs

Workflow run status|Start time|End time Succeeded|2016-06-04 13:14:11|2016-06-04 13:20:37 

1 Comment

I think I will go with your code ,works fine as well. @glenn jackman
0

Here is the final working code to grep & format the date

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.