Issue
I have a string like this:
"Execution error for request 13128790. Reason: ESS-07033 Job logic indicated a system error occurred while executing an asynchronous java job for request 13128790. Job error is: JBO-29000: Unexpected exception caught: java.lang.NumberFormatException, msg=null."
And, what I am trying to do is both extract specific IDs and templatize the original string so it can be normalized and reconstituted if needed.
Here is my sed file, which gets me close but doesn't seem to replace all of the occurrences of {id}
(13128790)
s/(.*)(ESS-[0-9]{5})(.*)/\2,\1{ess_code}\3/g
s/(.*)(JBO-[0-9]{5})(.*)/\2,\1{job_code}\3/g
s/(.*)([0-9]{8})(.*)/\2,\1{id}\3/g
current output
13128790,JBO-29000,ESS-07033,"Execution error for request 13128790. Reason: {ess_code} Job logic indicated a system error occurred while executing an asynchronous java job for request {id}. Job error is: {job_code}: Unexpected exception caught: java.lang.NumberFormatException, msg=null."
desired output:
13128790,JBO-29000,ESS-07033,"Execution error for request {id}. Reason: {ess_code} Job logic indicated a system error occurred while executing an asynchronous java job for request {id}. Job error is: {job_code}: Unexpected exception caught: java.lang.NumberFormatException, msg=null."
copy and paste example:
"Execution error for request 13128790. Reason: ESS-07033 Job logic indicated a system error occurred while executing an asynchronous java job for request 13128790. Job error is: JBO-29000: Unexpected exception caught: java.lang.NumberFormatException, msg=null." |
sed -e 's/(.*)(ESS-[0-9]{5})(.*)/\2,\1{ess_code}\3/g' \
-e 's/(.*)([0-9]{8})(.*)/\2,\1{id}\3/g' \
-e 's/(.*)(JBO-[0-9]{5})(.*)/\2,\1{job_code}\3/g'
Solution
Using sed
$ cat script.sed
s/([^0-9]* )([0-9]+)(.*)\2/\2,\1{id}\3{id}/g #Locate integers after a space and store within parenthesis to be returned later with back-referencing. If the same integer pattern is found, exclude it. Place the integer at the start of the string and replace all found matches with id
s/(,[^:]*: )([^ ]*)/,\2\1{ess_code}/ #Match from the comma inserted in the previous command up to a semi-colon and store in the first parenthesis. Capture the ess_code in the second parenthesis, replace with literal ess_code and move match to start of line with back-reference
s/([^,]*,)(([^:]*:){2} )([^:]*)/\1\4,\2{job_code}/ #Finally, match the first comma and isolate so as to be able to insert the match in the correct position, this time, match up to the semi-colon twice {2} and store in parentheses, grab the needed match in the 4th parenthesis and move behind the first match to align with the positioning in sample, replace match with literal {job_code}
$ sed -Ef script.sed input_file
Or as a single line
$ sed -E 's/([^0-9]* )([0-9]+)(.*)\2/\2,\1{id}\3{id}/g;s/(,[^:]*: )([^ ]*)/,\2\1{ess_code}/;s/([^,]*,)(([^:]*:){2} )([^:]*)/\1\4,\2{job_code}/' input_file
Output
13128790,JBO-29000,ESS-07033,"Execution error for request {id}. Reason: {ess_code} Job logic indicated a system error occurred while executing an asynchronous java job for request {id}. Job error is: {job_code}: Unexpected exception caught: java.lang.NumberFormatException, msg=null."
Answered By - sseLtaH Answer Checked By - Willingham (WPSolving Volunteer)