LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 08-19-2010, 09:28 PM   #1
wick_chad
LQ Newbie
 
Registered: Aug 2010
Posts: 3

Rep: Reputation: 0
grep for variable in a bash script


simple problem, I just cant seem to figure it out.. I've tried every combination of ' " \ that I have come across in similar threads on the forum but no luck..

I have 2 files
strings.txt: contains a list of numbers, 4 digits per line
file.txt: contains a lines that I want to grep for the strings.

for example:
>cat strings.txt
3214
8746
2411

>cat file.txt
freds phone number is 2411
janes phone number is 8746
carls phone number is 3214

this is what I'm using, but it isnt working.
Code:
#!/bin/bash
for i in $`cat strings.txt`; do
   grep "$i"  file.txt
done
if I echo $i in the loop, I print out the contents of strings.txt
If I put 3214 in place of grep "<$i>" file.txt I get "carls phone number is 3214"

but, together, it isnt doing anything...

thanks
-wick
 
Old 08-19-2010, 09:38 PM   #2
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Code:
grep -F -f strings.txt file.txt
or more precisely:
Code:
grep -f <(sed 's/.*/ &\$/' strings.txt) file.txt
 
Old 08-19-2010, 10:00 PM   #3
wick_chad
LQ Newbie
 
Registered: Aug 2010
Posts: 3

Original Poster
Rep: Reputation: 0
thanks for the prompt response.

unfortunately I tried that with no luck.. nothing gets outputted
Code:
$ cat strings.txt
5371
5343
5213
5037
5007
4840
4806
$ cat file.txt
andys phone number is  5371
cathys phone number is  5343
janes phone number is  5213
freds phone number is 5037
brads phone number is  5007
$ grep -F -f strings.txt file.txt
$ grep 5371 file.txt
andys phone number is  5371
$ grep -f <(sed 's/.*/ &\$/' strings.txt) file.txt
$
 
Old 08-19-2010, 10:08 PM   #4
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
You sure the files are in UNIX format? The command works fine with me:
strings.txt
Code:
3214
8746
2411
file.txt
Code:
freds phone number is 2411
janes phone number is 8746
carls phone number is 3214
command
Code:
grep -f <(sed 's/.*/ &\$/' strings.txt) file.txt
output
Code:
freds phone number is 2411
janes phone number is 8746
carls phone number is 3214
info: http://kb.iu.edu/data/acux.html
 
Old 08-19-2010, 10:17 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Gotta back konsolebox, works for me ... I even tried it without the sed and all worked fine:
Code:
grep -f strings.txt file.txt
Obviously the diff is that this mode will allow the strings to appear anywhere, so not as concise but might help for testing.
 
1 members found this post helpful.
Old 08-20-2010, 08:40 AM   #6
wick_chad
LQ Newbie
 
Registered: Aug 2010
Posts: 3

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by konsolebox View Post
You sure the files are in UNIX format? The command works fine with me:
that was it!!
I had created the file strings.txt on a windows PC and saved it to linux.. once I ran dos2unix everything worked. I didnt realize the end of line differences would create a problem..

MUCHAS GRACIAS
 
Old 08-20-2010, 10:16 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Glad you have your solution, please mark as SOLVED.
 
  


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
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 04:11 AM
Pipe a variable using grep in a script. okos Linux - Software 6 08-17-2009 02:31 AM
Problem with bash script - variable name within variable name steven.c.banks Linux - Newbie 3 03-10-2009 03:08 AM
grep a shell script variable contents kushalkoolwal Programming 8 02-04-2009 06:15 AM
Bash Script Help - Trying to create a variable inside script when run. webaccounts Linux - Newbie 1 06-09-2008 02:40 PM

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

All times are GMT -5. The time now is 09:39 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
Open Source Consulting | Domain Registration