Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 02-01-2008, 06:52 AM   #1
LQ Newbie
Registered: Feb 2008
Posts: 17

Rep: Reputation: 0
"searching for a matching pattern"


i am trying to search for a particular word form a file.
My code is not working. Please help

cat filename | while read line
for i in $line ;do
if [ "i" == "world" ]; then
echo "found"

Here i am first reading a line and then comparing each world of the line with the word "world".
Old 02-01-2008, 07:03 AM   #2
Senior Member
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,293

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335

You are missing a $ before i:

if [ "$i" == "world" ]; then

And by the way, why not use grep?
Old 02-01-2008, 07:12 AM   #3
LQ Newbie
Registered: Feb 2008
Posts: 17

Original Poster
Rep: Reputation: 0
thanks for my code is working
Old 02-01-2008, 07:18 AM   #4
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
cat filename | while read line
Does this work? A pipe is used to pass output to a command that expects input. I don't know what it means to pass data to the "while" command.

"read" means get data from standard input (the keyboard unless changed)

for i in $line; do
This only loops once (if at all). The "i in" construct has to be followed by something that produces a list of values.
Old 02-01-2008, 07:23 AM   #5
Senior Member
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Originally Posted by pixellany View Post
Does this work?
yes it does. The "better" way is to skin the cat totally.
while read -r line
  # blah
done < "file"
Old 02-01-2008, 09:06 AM   #6
Registered: Sep 2006
Location: Durham, NC
Distribution: Slackware, Ubuntu (yes, both)
Posts: 463

Rep: Reputation: 31
Why are you using this instead of grep?

Old 02-02-2008, 12:24 PM   #7
Registered: Jul 2006
Location: Maryland, USA
Distribution: Fedora, PCLinuxOS
Posts: 666

Rep: Reputation: 91
Two people have already asked about grep, so forgive me, but I just can't help but suggest this:

grep -wq world filename && echo found


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
unclean shutdown causes "Couldn't find matching filesystem: label=" error irnerdette Red Hat 3 06-02-2006 02:44 PM
Pattern matching with a "case" statement in BASH King V Programming 8 04-27-2006 05:19 PM
Im searching software like "ASE" and "Fraps" Krilintar Linux - Software 2 12-27-2005 11:39 AM
dump + restore to backup linux ; "Couldn't find matching filesystem: LABEL=/" on boot markus1982 Linux - General 4 09-21-2002 02:16 PM > Forums > Non-*NIX Forums > Programming

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

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