LinuxQuestions.org
Visit Jeremy's Blog.
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-01-2008, 05:52 AM   #1
sharath.bv
LQ Newbie
 
Registered: Jan 2008
Posts: 17

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


hi,

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

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

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

Rep: Reputation: 236Reputation: 236Reputation: 236
Hi

You are missing a $ before i:

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

And by the way, why not use grep?
 
Old 02-01-2008, 06:12 AM   #3
sharath.bv
LQ Newbie
 
Registered: Jan 2008
Posts: 17

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

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Quote:
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)

Quote:
for i in $line; do
..
done
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, 06:23 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,696
Blog Entries: 5

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

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

Joel
 
Old 02-02-2008, 11:24 AM   #7
KenJackson
Member
 
Registered: Jul 2006
Location: Maryland, USA
Distribution: Fedora, Arch
Posts: 572

Rep: Reputation: 64
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
 
  


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


All times are GMT -5. The time now is 01:14 AM.

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