Reputation: 1790
I'm trying to grep a word in a file.txt inside "<" and ">" for two days but impossible to get it. For example, i've something like that in a file.txt :
30993250 (<[email protected]> 2014-09-22 12:41:56 +0200 365)
It come from a git blame -L 365,+1 path\to\file.cpp-e
and i want to only recover the email inside. I'm trying this following code :
grep -oP '(?<=\<)\w+(?= \>)' ..\log\file.txt > ..\log\result.txt
and
grep -Po '(?<=\().*(?=\))' ..\log\file.txt > ..\log\result.txt
But it doesn't works and says the syntax of file is incorrect or the specified path was not found. I don't understand, I've tried lots of combinations but nothings works...
I want to say i'm using batch under Windows. Is there somebody who can help me please ?
EDIT : I found a solution where i use the ASCII table like that :
grep -oP '["\074].*["\076]' ..\log\file.txt > ..\log\result.txt
But display the "<>" characters like this :
How i can remove that ?
Thanks
Upvotes: 0
Views: 198
Reputation: 130839
The perl mode -P
allows look behind. Note - I'm using GNU grep for Windows.
grep -P -o "(?<=<)[^>]+(?=>)" ..\log\file.txt >..\log\result.txt
For Windows batch users that don't have access to grep (some companies disallow 3rd party exe programs), you could use my JREPL.BAT - a pure script regex search/replace utility that runs natively on Windows.
jrepl "<([^>]+)>" $1 /jmatch /f ..\log\file.txt /o ..\log\result.txt
Upvotes: 1
Reputation: 82307
grep will find and show lines, it will not remove anything from a line.
But in your case you can read the result and remove the <>
characters.
grep -oP '["\074].*["\076]' ..\log\file.txt > ..\log\result.txt
setlocal EnableDelayedExpansion
set /p line= < ..\log\result.txt
set "line=!line:<=!"
set "line=!line:>=!"
echo !line!
Upvotes: 1