I’m trying to run a regex over an XML file (Jira export file, to be precise) to fix an issue with some JQL-queries in it.
I’m looking for an attribute called “request”, it contains a query. In that query, usernames need to be wrapped in HTML-entities for quotes ("quot;).
A username always follows the string “reporter = ‘, “assignee = “, or “watcher = “, except for one special case: the string currentUser() is not a username and does not need to be replaced.
Original:
<SearchRequest id="10000" name="Example" author="myusername" user="myusername" request="reporter = anotheruser and status != Closed" favCount="1"/>
Result:
<SearchRequest id="10000" name="Example" author="myusername" user="myusername" request="reporter = "anotheruser" and status != Closed" favCount="1"/>
Search:
(request=".*?(reporter|assignee|watcher) = )(?!currentUser)([a-z.]+)(.*?")
Replace:
$1&$3&$4
This has been tried and tested in SublimeText 2, and Regex Tester 2, and works correctly. As you can see, it uses a lookahead to detect the negative case for currentUser. Now, when I try to use this regex in Sed, it errors out:
$ sed -i '' -E 's/(request=".*?(reporter|assignee|watcher) = )(?!currentUser)([a-z.]+)(.*?")/$1&$3&$4/g' entities.xml
sed: 1: "s/(request=".*?(reporte ...": RE error: repetition-operator operand invalid
I’m not sure how to proceed now, as sed is new territory to me. I’m inclined to think it’s the lookahead that causes this problem. Perhaps there’s a simpler way to fulfil this requirement?
Unfortunately lookaheads/behinds are not support in
sedyou should take a lot atssed(super sed). Here is the FAQ if you want more information.Also this could be done in
perlif you are familiar, which I am not (I tagged with perl so you should get some help).Using the
-Poption withgrepverifies your match: