LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-04-2016, 01:43 PM   #1
Jason_25
Member
 
Registered: Nov 2001
Posts: 180

Rep: Reputation: 23
Use variable instead of text file with grep -f (file mode)?


This does not work:
Code:
#dictation words variable
INPUT="i fine"

#convert to variable with new lines instead of space
PROCESSED_INPUT=$(echo $INPUT | tr ' ' '\n')

#searches multiple lines for the most words in the variable and orders them
#ai dictionary can be a zeroed file for these purposes
cat AI_DICTIONARY | grep -Ff $PROCESSED_INPUT
This works:
Code:
#dictation words variable
INPUT="i fine"

#convert to file with new lines instead of space
echo $INPUT | tr ' ' '\n' > INPUT_TEST

#searches multiple lines for the most words in the input file and orders them
#ai dictionary can be a zeroed file for these purposes
cat AI_DICTIONARY | grep -Ff INPUT_TEST
How can I do this in memory instead of with files?

Last edited by Jason_25; 12-05-2016 at 10:21 AM.
 
Old 12-04-2016, 02:25 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Try quoting your variables to maintain the spacing.
 
1 members found this post helpful.
Old 12-04-2016, 03:10 PM   #3
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
With this AI_DICTIONARY ...
Code:
i fine
you fine
he fail
i good
she fine
Herman happy
Melvin euphoric
... this code ...
Code:
#dictation words variable
INPUT="i fine"

#convert to file with new lines instead of space
tr ' ' '\n' <$INPUT >$INPUT_TEST

#searches multiple lines for the most words in the input file and orders them
#ai dictionary can be a zeroed file for these purposes
grep -wFf $INPUT_TEST <$AI_DICTIONARY >$OutFile
... produced this OutFile ...
Code:
i fine
you fine
i good
she fine

With the same AI_DICTIONARY this code ...
Code:
#dictation words variable
INPUT="i fine"

#convert to variable with new lines instead of space
PROCESSED_INPUT=$(tr ' ' '|' <<<$INPUT)

#searches multiple lines for the most words in the variable and orders them
#ai dictionary can be a zeroed file for these purposes
egrep -w "($PROCESSED_INPUT)" <$AI_DICTIONARY >$OutFile
... produced the same OutFile.


If you care for a further refinement it is possible to eliminate the variable PROCESSED_INPUT.
With the same AI_DICTIONARY this code ...
Code:
#dictation words variable
INPUT="i fine"

#convert to variable with new lines instead of space
#searches multiple lines for the most words in the variable and orders them
#ai dictionary can be a zeroed file for these purposes
egrep -w "($(tr ' ' '|' <<<$INPUT))" <$AI_DICTIONARY >$OutFile
... produced the same OutFile.

Daniel B. Martin
 
Old 12-05-2016, 10:17 AM   #4
Jason_25
Member
 
Registered: Nov 2001
Posts: 180

Original Poster
Rep: Reputation: 23
I still have not figured this out. It may not be possible. The problem appears to actually be with grep -f not grep -F. The list feature appears to not take a variable.

The error is
Code:
grep: i: no such file or directory
There are ways to do what I want without providing a list but I prefer how simple it is.

grail - quoting variables helped but was not the primary problem.

danielbmartin - I always get
Code:
$Outfile: ambiguous redirect
with that code.

edit:
I think i got it to work. I am just still in shock because it was so simple. Use e instead of f.
Code:
cat AI_DICTIONARY | grep -Fe $PROCESSED_INPUT

Last edited by Jason_25; 12-05-2016 at 10:30 AM.
 
Old 12-05-2016, 10:46 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Well, that would make sense as your data is no longer coming from a file

Don't forget to mark as SOLVED now you have a solution.
 
Old 12-05-2016, 11:34 AM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,736

Rep: Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920
You should not use -fF. -f expects a file. -F is a list.

Code:
#dictation words variable
INPUT="i fine"

#convert to variable with new lines instead of space
PROCESSED_INPUT=$(echo $INPUT | tr ' ' '\n')

#searches multiple lines for the most words in the variable and orders them
#ai dictionary can be a zeroed file for these purposes
grep -F $PROCESSED_INPUT AI_DICTIONARY
It isn't necessary to use cat...

Last edited by michaelk; 12-05-2016 at 11:38 AM.
 
Old 12-05-2016, 12:33 PM   #7
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,805

Rep: Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206
Code:
INPUT="i fine
i good"

grep -F "$INPUT" AI_DICTIONARY
Note that $INPUT must be in "quotes"! Previous posts forgot that.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shell: copy files based on two strings at fixed locations in file contents tankwart Programming 2 10-09-2014 11:25 AM
grep query to list 1 line [which is fixed] and the next line after it [variable text] Glenn D. Linux - Software 3 01-20-2011 06:21 AM
Sed/awk/grep search for number string of variable length in text file Alexr Linux - Newbie 10 01-19-2010 01:34 PM
How to grep two strings in one line in a file? thomas2004ch Linux - Newbie 5 09-15-2009 04:49 AM
How to store text(strings) in a 2D character array reading from a text file(C++) bewidankit Programming 3 02-14-2008 07:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration