LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 09-16-2013, 10:45 AM   #1
socalheel
Member
 
Registered: Oct 2012
Location: Raleigh, NC
Distribution: CentOS / RHEL
Posts: 154

Rep: Reputation: 3
assign full name to variable then grep a file for the variable


i have a file that has multiple names in it named names.txt. the file contents are as follows

"james spader"
"james tidwell"
"brian stern"
"rachel thurston"
"bob denver"
"todd greenberg"

how would i grep a different file for the names within names.txt

here is the command i have but it greps the file for every instance of james, spader, james, tidwell, brian, stern, and not the full name (i.e. "james spader" "james tidwell" "brian stern" etc.)

Code:
for i in `cat names.txt`; do grep -i $i names_two.txt; done
 
Old 09-16-2013, 10:52 AM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,281

Rep: Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916
Code:
cat names.txt | while read i; do grep -i "$i" names_two.txt; done
#david-the-h
 
Old 09-16-2013, 11:03 AM   #3
socalheel
Member
 
Registered: Oct 2012
Location: Raleigh, NC
Distribution: CentOS / RHEL
Posts: 154

Original Poster
Rep: Reputation: 3
you da man.

i found out about while read i after i posted this but couldn't get that working 100% either ... the "" in do grep -i "$i" did the trick.

thanks bro.
 
Old 09-16-2013, 11:04 AM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,559

Rep: Reputation: 1854Reputation: 1854Reputation: 1854Reputation: 1854Reputation: 1854Reputation: 1854Reputation: 1854Reputation: 1854Reputation: 1854Reputation: 1854Reputation: 1854
Code:
grep -iFf names.txt names_two.txt
Quote:
Originally Posted by schneidz
#david-the-h
You can't use that on a post containing a useless use of cat.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Bash script: How to assign variable to an sqlite3 command with variable embedded? ninja6o4 Linux - Software 10 02-15-2015 04:43 PM
[SOLVED] BASH: Assign a variable to a variable name... c_henry Programming 9 06-04-2012 11:33 AM
grep a file for any interger larger than X and assign largest value to a variable kmkocot Linux - Newbie 5 11-09-2009 05:26 PM
How do you assign a variable to be a variable file name in a directory? David_Elliott Programming 4 04-14-2009 10:19 AM
how to select a particular line from a file and assign it to a variable? pdklinux79 Linux - Newbie 4 06-18-2008 07:21 PM

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

All times are GMT -5. The time now is 10:31 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration