GNU bug report logs -
#24025
grep usage question
Previous Next
Full log
Message #10 received at control <at> debbugs.gnu.org (full text, mbox):
[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)]
This bug report was last modified 8 years and 300 days ago.
Previous Next
GNU bug tracking system
Copyright (C) 1999 Darren O. Benham,
1997,2003 nCipher Corporation Ltd,
1994-97 Ian Jackson.