问题
I have several large log files that include lines in the following format:
/resource/text_(moretext
Now these need to be closed with a ")", but the file is way too large to do this manually. Unfortunately the text within the lines can be anything. So I think I need some expression that is able to find all lines that have "(" and no ")". Then these lines have to be replaced with the exact same content but with ")" added to the end.
So it should look like this:
Before:
/resource/text_(moretext
After:
/resource/text_(moretext)
I feel like this should be possible in Notepad++ using regular expressions, but I have a hard time figuring out how to do this.
I need this because I am comparing these logs to a .TTL file to extract the lines that can be found in both files. I do this using the following AWK script:
BEGIN { IGNORECASE = 1 } # ignoring the case
NR==FNR { a[$1]; next } # hash csv to a hash
{
for(i in a) { # each entry in a
if($0 ~ i) { # check against every record of ttl
print >> "testrunawk1.txt" # if match, output matched ttl record
next # skip to next ttl record
}
}
}
Right now I get the following error on all these lines when I run the AWK script:
Fatal: unmatched ( or \(: //resource/text_(moretext/
Thank you very much for the help :)
回答1:
Using sed with -E option you could do:
sed -E 's/\([^)]+$/\0)/' file
With Notepad++ you can do the same with this difference that you should include newline character preferably with \r
in character class.
回答2:
I believe this should get you most of the way there.
Find: ([^\(]*\([^\(]*)\n
Replace: $1\)\n
In your find the unescaped brackets mark a group to be captured (the text you want to keep) which is placed in the replace by the $1
The \
escapes a character so \(
will pick up an opening bracket in the text instead of marking a capture in the expression.
Square brackets mark a set of characters to match with the ^
character marking that you want to match anything that is NOT in the group. so [^\(]
matches anything that is NOT an opening bracket. This group is followed by a *
which quantifies 0 or more matches.
Finally the \n
marks a new line. This may not be the correct character for your line endings depending on the format. You may need to use something like \r\n
instead.
I find a helpful resource for building regex queries is https://regexr.com You can paste in a sample of your text and try out some matches.
Hope this helps.
来源:https://stackoverflow.com/questions/49753347/notepad-find-all-lines-with-open-parentheses-but-no-close-parentheses-and-add