LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 02-24-2008, 09:30 AM   #1
papasj
LQ Newbie
 
Registered: Apr 2005
Posts: 9

Rep: Reputation: 0
grep from filename.txt can't get the whole line


Hi,

I've made a script to grep a file for
strings that are taken from a filename.txt

Code:
for i in `cat filename.txt`
do
    strings ./binfile | grep "$i" 2>&1 > /dev/null
done
this works fine as long as in filename.txt i don't have any entries with spaces. But in my case i want to grep something with spaces like "lala tata" and the above script greps for lala and then tata seperately.

When I use while read i have the same problem.
any ideas anyone??

thanks,
papasj
 
Old 02-24-2008, 10:00 AM   #2
papasj
LQ Newbie
 
Registered: Apr 2005
Posts: 9

Original Poster
Rep: Reputation: 0
the solution is to set IFS to a newline character before executing the loop like


IFS="
"
 
Old 02-24-2008, 11:24 AM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,965
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
Why don't you just use
Code:
strings ./binaryfile | grep -f filename.txt
?


Cheers,
Tink
 
Old 02-25-2008, 12:38 PM   #4
Big_Vern
LQ Newbie
 
Registered: Jan 2008
Posts: 9

Rep: Reputation: 1
Quote:
Originally Posted by papasj View Post
Hi,

I've made a script to grep a file for
strings that are taken from a filename.txt

Code:
for i in `cat filename.txt`
do
    strings ./binfile | grep "$i" 2>&1 > /dev/null
done
this works fine as long as in filename.txt i don't have any entries with spaces. But in my case i want to grep something with spaces like "lala tata" and the above script greps for lala and then tata seperately.

When I use while read i have the same problem.
any ideas anyone??

thanks,
papasj

Does the following work?

Code:
while read line
do
 strings ./binfile | grep "$line" 2>&1 >/dev/null
done < filename.txt
Changing the IFS should also work but remember to set it back to what it was once you've finished.

Last edited by Big_Vern; 02-25-2008 at 12:39 PM. Reason: Arghh forgot the code tags :)
 
  


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
shell script find a line and the next line (grep?) metalx1000 Programming 5 07-24-2007 08:41 PM
using commands to output path, filename and info to a txt file bob_man_uk Linux - General 3 05-11-2006 02:31 PM
bash: how to edit cat <filename> | grep <keyword> feature? sirpelidor Linux - Software 2 06-20-2005 02:00 PM
Read a line in a txt file with bash orgazmo Programming 5 05-03-2005 07:10 AM
Read a line in a txt file with bash orgazmo Linux - Newbie 3 05-03-2005 04:16 AM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration