Algorys
Algorys

Reputation: 1790

Get the text inside "<" and ">" with grep in batch

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 :

<[email protected]>

How i can remove that ?

Thanks

Upvotes: 0

Views: 198

Answers (2)

dbenham
dbenham

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

jeb
jeb

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

Related Questions