Subject: Re: bug#20605: Grep command

From: Eric Blake <[email-masked]>
Date: Mon, 18 May 2015 20:17:13 -0600

On 05/18/2015 11:28 AM, Lakshmana Rao Adabala, Shanmukha wrote:
> Hi Team,
> 
> I would like to know how to get the grep output in the same order.
> 
> For example:
> 
> I am using the following order :  04A2,17F5,04A4
> 

Screenshots are hard to parse; it's better to paste actual screen text
content.

It looks like you are using Windows, due to the unusual quoting
paradigms used on the command line:

service@VPLEXCLU2:~> cat 852* | grep -E ""04A4"|"17F5"|"04A2""

On a Unix system, the quoting would be:
cat 852* | grep -E "04A4|17F5|04A2"

> 
> I am expecting output in the following order.
> 
> 04A2
> 17F5
> 04A4

It sounds like you were hoping that grep would make three passes over
your input stream (the concatenated 852* files) - first outputting all
matches to the pattern 04A4, then all matches to the pattern 17F5, then
all matches to the pattern 04A2.  But that's not how grep works.  It
makes a single pass over your input, and for each line, it checks all
alternations within the pattern at once.

> 
> But I am getting the output in the below order
> 
> 04A2
> 04A4
> 17F5

Which means that the first line that matched is one that matched your
third alternative in the single pattern.

> 
> Please let me if any additional switches to use.

The only way to get output in a particular order is to do three separate
passes of grep, on one non-alternating pattern each, rather than one
pass with a three-alternation pattern.

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