Wednesday, 21 August 2013

How to match a non string in gnu grep

How to match a non string in gnu grep

I'll use an example to illustrate my problem. Suppose we have the file
name 'file.txt' that contains the following string:
AooYoZooYZoAoooooYZ
I'd like to use grep to find all substrings that begin with 'A' and end
with 'YZ' but do not contain 'YZ' in between the 'A' and 'YZ'. The desired
output would be:
AooYoZooYZ
AoooooYZ
My best guess is to do the following:
$grep -E -o 'A[^(YZ)]*YZ' file.txt
But the output is only:
AoooooYZ
I'd like the parentheses to hold their meaning for the YZ but I read in
the GNU grep manual (http://www.gnu.org/software/grep/manual/grep.html)
that: "Most meta-characters lose their special meaning inside bracket
expressions." I've also tried:
$grep -E -o 'A.*YZ file.txt
But this outputs the entire line:
AooYoZooYZoAoooooYZ
Is there a way to override this or another way of solving my problem?

No comments:

Post a Comment