Computing.Net > Forums > Unix > Search for words in a text file

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

Search for words in a text file

Reply to Message Icon

Original Message
Name: Mitesh
Date: September 17, 2007 at 04:09:15 Pacific
Subject: Search for words in a text file
OS: Unix
CPU/Ram: Intel, 512MB
Manufacturer/Model: Dell
Comment:

Hi,
I am a unix newbie.

I am trying the following to search for a number of words in a text file (@unix command prompt), but the entire file contents are displayed. Can anyone tell me where I am making a mistake? Thanks Mitesh

for i in `cat /tmp/mylist`
> do
> grep i /etc/passwd
> done

mylist contains Users names.

Thanks
Mitesh



Report Offensive Message For Removal


Response Number 1
Name: nails
Date: September 17, 2007 at 07:24:39 Pacific
Reply: (edit)

#!/bin/ksh

# untested as I'm not near a unix box and
# I'm assuming there's one word per line

while read i
do
grep "$i" /etc/passwd
done < /tmp/mylist


Report Offensive Follow Up For Removal

Response Number 2
Name: Mitesh
Date: September 19, 2007 at 01:54:02 Pacific
Reply: (edit)

Ah silly me, I was forgetting to add the '$' sign before the variable.. It now works. Thanks mate!

Mitesh


Report Offensive Follow Up For Removal







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home








Do you have your own blog?

Yes
No
I did before
I will soon


View Results

Poll Finishes In 2 Days.
Discuss in The Lounge
Poll History




Data Recovery Software