Regex search with grep: How to get only search pattern as result and not whole line?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ensign
Posts: 3
Joined: 08 Sep 2017 07:58

Regex search with grep: How to get only search pattern as result and not whole line?

#1 Post by ensign » 14 Sep 2017 07:00

Hello,

may I ask if it is possible to get grep.exe to return only result of seach pattern and not any (complete) lines which contain the seacht pattern?

(I'm using Gnu Grep V2.5.4 for Windows 7)

Thanks for any hint.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Regex search with grep: How to get only search pattern as result and not whole line?

#2 Post by aGerman » 14 Sep 2017 14:33

Of course. And if this question is related to your first thread then it might work like that:

Code: Select all

grep -P "\bA_[A-Za-z\d]+_f[ ]*\([ ]*[-]*[\d.]+[ ]*\)" -o "test.txt"

Steffen

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Regex search with grep: How to get only search pattern as result and not whole line?

#3 Post by ShadowThief » 14 Sep 2017 21:13

I seem to recall grep having a -o flag that only returned the matching part.

EDIT: Yep, -o. https://www.gnu.org/software/grep/manua ... ut-Control

Post Reply