rkj
rkj

Reputation: 711

string parsing & date formatting unix

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?

Upvotes: 2

Views: 397

Answers (3)

glenn jackman
glenn jackman

Reputation: 246754

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

Upvotes: 0

rkj
rkj

Reputation: 711

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'

Upvotes: 0

Rahul
Rahul

Reputation: 420

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'

Upvotes: 2

Related Questions