1

I am trying to read records from file and if string is available in any files then print that file. code:

while read line
do
find ./q/q*.q -type f -exec grep -ls $line {} +
donr< "file_name"

file_name have data like:

india
usa_k
in_va

while execution i am getting grep: illegal option error

Same time I want to store o/p of find in variable. Any help

in loop only i want to concate $line and find o/p and direct in txt file.

code some like:

$line | o/p find >> missing.txt

In i/p file if we have value "INDIA" if this value is aviable in other txt file then i want to print "india is aviable in (filename)" and store this information in txt file. this we need to do for all values..

Deepak
  • 27
  • 7
  • What is the content of `file_name`? Both `-l` and `-s` seem reasonable flags for `grep`, but `$line` may contain some extra flags that confuse `grep`. – fedorqui Aug 29 '18 at 12:09
  • added information in question. – Deepak Aug 29 '18 at 12:15
  • BTW, the "how do I store output in a variable?" part of this question is arguably duplicative of other contents already in the knowledgebase; for example, https://stackoverflow.com/questions/4651437/how-to-set-a-variable-to-the-output-of-a-command-in-bash -- that's part of why we try to insist that questions be asked with narrow scope, to avoid overlap between them and thus potential for conflicting answers. – Charles Duffy Aug 29 '18 at 12:20
  • Do you mean that the lines you're finding are themselves shell commands? Show some examples. – Charles Duffy Aug 29 '18 at 12:45
  • in i/p file if we have value "INDIA" if this value is aviable in other txt file then i want to print "india is aviable in (filename)" and store this information in txt file. this we need to do for all values.. I hope this give you clear info. Thank you – Deepak Aug 29 '18 at 12:50
  • BTW, this has gotten rather far beyond the initial scope (which was focused on an "illegal option" errors from `grep`). See discussion of "chameleon questions" in [What to do when someone answers: Don't be a chameleon, don't be a vandal](https://meta.stackoverflow.com/questions/332820/what-to-do-when-someone-answers-dont-be-a-chameleon-dont-be-a-vandal) on [meta]. – Charles Duffy Aug 29 '18 at 15:40

1 Answers1

1

Put -- before your names as an end-of-option terminator, -e before the expansion of "$line", and quote that expansion. This prevents either the names or the search string from being read as options.

while IFS= read -r line; do
  find ./q/q*.q -type f -exec grep -l -e "$line" -- {} + |
    while IFS= read -r matched_file; do
      echo "$line is available in $matched_file"
    done
done <file_name >results.txt
fedorqui
  • 275,237
  • 103
  • 548
  • 598
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • "--" is working correctly but unable to store output in variable .. in loop it self i want to use output of $line and find output in txt. Like $line | o/p of find >> missinf.txt – Deepak Aug 29 '18 at 12:35
  • in i/p file if we have value "INDIA" if this value is aviable in other txt file then i want to print "india is aviable in (filename)" and store this information in txt file. this we need to do for all values.. I hope this give you clear info. Thank you – Deepak Aug 29 '18 at 12:50