Subject: Re: bug#24858: URGENT: Question about grep

From: Eric Blake <[email-masked]>
Date: Wed, 2 Nov 2016 12:00:21 -0500

On 11/02/2016 10:52 AM, Bruce Dubbs wrote:

>>
>> I want that the research is done only in the underline characters.  So
>> what I have to add in grep command to put the limit of 30 characters?
> 
> cut -c 30 filename | grep ACGTAC

That works if you are only interested in seeing the first 30 characters
of a given line, rather than printing the entire line when the match was
only within the first 30 characters.  If you need to map back to the
entire line, you can use some sort of decorate-search-undecorate
algorithm to keep the search portion still under grep, but at that
point, it's probably easier to just write it all in a language that can
do it in a single pass.

I guess I should also mention that if you know your lines are a fixed
width (say for example that every line is exactly 80 characters), then
you can exploit that using just grep to find a match only in the first
30 characters by explicitly spelling out the fixed-width remainder of
the line as an anchor:

grep 'ACGTAC.*.\{50\}$' filename

Sadly, the two example lines you printed were not the same length, so I
don't think it helps for your case.

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org