Subject: Re: bug#24025:

From: Eric Blake <[email-masked]>
Date: Tue, 19 Jul 2016 10:06:04 -0600

retitle 24025 grep usage question
tag 24025 needinfo
thanks

On 07/19/2016 06:11 AM, Leroy Harrill wrote:
> I am not sure if this is a bug or not. Her is an example of my script that
> I am running.
> 

Thanks for the report.  However, you haven't given us enough details to
reproduce the problem you are seeing (such as the contents of file1 or
of some_file.txt), nor even what you are expecting to see.  You also
didn't set a very informative subject line.

> #!/bin/bash
> 
> rm -f x.txt
> rm -f new_file
> 
> i=1
> wc -l file1 | sed 's/file1//g' >> x.txt
> 
> x=$(<x.txt)

Wow, that's an inefficient way to set x.  Why not just:

x=$(wc -l < file1)

and completely skip the use of x.txt.

> 
> while [ $i -le $x ]
> do
>             #echo "$i"
>          A=$( awk -v var="$i" 'NR==var {print}' file1)

You can achieve the same goal of extracting a single line from a file
with less typing, via:

A=$(sed -n "$i p" file1)

>         #ack -k file1 file2 --match $A >> new_file
>         grep $A some_file.txt
>     ((i++))

You probably want to use "$A", not unquoted $A, even if you are
absolutely sure that every line of file1 is a single word that won't be
corrupted by the shell if the variable is reused unquoted.

Is your intent to grep for multiple patterns in some_file.txt, where
each pattern occupies a different line of file1?  In which case, can't
you just ditch the shell loop entirely, and write:

grep -f file1 some_file.txt

which will then give you a list of all lines in some_file.txt that match
any of the patterns in file1 (albeit not necessarily in the same order
as your shell loop would have given)?

> 
> done
> 
> grep does not appear to be grepping inside of the while loop. When I
> initialize a variable directly from the command line and write the same
> script it works no problem, any help on this matter would be greatly
> appreciated.

You'll have to provide more details for us to be able to reproduce your
situation; most likely it is not a bug in grep but a problem with your
shell script, but I'll wait to close out this bug in the grep database
until we have more information.

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