GNU bug report logs - #24025
grep usage question

Previous Next

Package: grep;

Reported by: Leroy Harrill <cdideveloper016 <at> gmail.com>

Date: Tue, 19 Jul 2016 15:38:01 UTC

Severity: normal

Tags: moreinfo

Done: Paul Eggert <eggert <at> cs.ucla.edu>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 24025 in the body.
You can then email your comments to 24025 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-grep <at> gnu.org:
bug#24025; Package grep. (Tue, 19 Jul 2016 15:38:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Leroy Harrill <cdideveloper016 <at> gmail.com>:
New bug report received and forwarded. Copy sent to bug-grep <at> gnu.org. (Tue, 19 Jul 2016 15:38:02 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Leroy Harrill <cdideveloper016 <at> gmail.com>
To: bug-grep <at> gnu.org
Date: Tue, 19 Jul 2016 08:11:09 -0400
[Message part 1 (text/plain, inline)]
I am not sure if this is a bug or not. Her is an example of my script that
I am running.

#!/bin/bash

rm -f x.txt
rm -f new_file

i=1
wc -l file1 | sed 's/file1//g' >> x.txt

x=$(<x.txt)

while [ $i -le $x ]
do
            #echo "$i"
         A=$( awk -v var="$i" 'NR==var {print}' file1)
        #ack -k file1 file2 --match $A >> new_file
        grep $A some_file.txt
    ((i++))

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.
[Message part 2 (text/html, inline)]

Information forwarded to bug-grep <at> gnu.org:
bug#24025; Package grep. (Tue, 19 Jul 2016 16:07:01 GMT) Full text and rfc822 format available.

Message #8 received at 24025 <at> debbugs.gnu.org (full text, mbox):

From: Eric Blake <eblake <at> redhat.com>
To: Leroy Harrill <cdideveloper016 <at> gmail.com>, 24025 <at> debbugs.gnu.org,
 GNU bug control <control <at> debbugs.gnu.org>
Subject: Re: bug#24025:
Date: Tue, 19 Jul 2016 10:06:04 -0600
[Message part 1 (text/plain, inline)]
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

[signature.asc (application/pgp-signature, attachment)]

Set bug title to 'grep usage question'. Request was from Eric Blake <eblake <at> redhat.com> to control <at> debbugs.gnu.org. (Tue, 19 Jul 2016 16:07:02 GMT) Full text and rfc822 format available.

Added tag(s) moreinfo. Request was from Eric Blake <eblake <at> redhat.com> to control <at> debbugs.gnu.org. (Tue, 19 Jul 2016 16:07:02 GMT) Full text and rfc822 format available.

Reply sent to Paul Eggert <eggert <at> cs.ucla.edu>:
You have taken responsibility. (Sat, 24 Sep 2016 21:39:02 GMT) Full text and rfc822 format available.

Notification sent to Leroy Harrill <cdideveloper016 <at> gmail.com>:
bug acknowledged by developer. (Sat, 24 Sep 2016 21:39:02 GMT) Full text and rfc822 format available.

Message #17 received at 24025-done <at> debbugs.gnu.org (full text, mbox):

From: Paul Eggert <eggert <at> cs.ucla.edu>
To: Leroy Harrill <cdideveloper016 <at> gmail.com>
Cc: 24025-done <at> debbugs.gnu.org
Subject: Re: grep usage question
Date: Sat, 24 Sep 2016 14:38:04 -0700
No further comment and it has been a couple of months, so closing the bug report.




bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Sun, 23 Oct 2016 11:24:03 GMT) Full text and rfc822 format available.

This bug report was last modified 8 years and 299 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.