LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-14-2009, 07:15 AM   #1
Coolsafe
LQ Newbie
 
Registered: Nov 2009
Posts: 4

Rep: Reputation: 0
Question How to find all lines in a file that contains a particular word


How to find all lines in a file that contains a particular word ? For example...let's say the word "from".
 
Old 11-14-2009, 07:23 AM   #2
tom4everitt
Member
 
Registered: Jan 2009
Location: Stockholm
Distribution: Fedora, Ubuntu, Mac OS X
Posts: 78

Rep: Reputation: 23
It depends a little on what kind of file you're searching, and the kind of output you want. Simply

Code:
more thefile | grep from
will print exactly the lines of "thefile" that contains the word from (given that the file is a plain text file). Just write back if this was not exactly what you were looking for.
 
Old 11-14-2009, 07:28 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
I would do it as:
Code:
grep from thefile | more
Cool;
Got to http://tldp.org and get a copy of the Bash Guide for Beginners
 
Old 11-14-2009, 07:56 AM   #4
Coolsafe
LQ Newbie
 
Registered: Nov 2009
Posts: 4

Original Poster
Rep: Reputation: 0
Thumbs up Simply cool

great. will check out bash 4 beginners. thks.
 
Old 11-14-2009, 12:39 PM   #5
tom4everitt
Member
 
Registered: Jan 2009
Location: Stockholm
Distribution: Fedora, Ubuntu, Mac OS X
Posts: 78

Rep: Reputation: 23
Yes, elementary bash knowledge is often extremely useful for a lot of stuff that has to do with linux in general and the terminal in particular. You can actually write entire programs exclusively in bash (although its not very convenient when they grow bigger).
 
Old 11-14-2009, 05:57 PM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,838

Rep: Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822
Strictly speaking that won't only get "words" being searched for.
You can also get grep to show the line number of successful hits.

All expounded in the man page.
 
Old 11-14-2009, 06:53 PM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by Coolsafe View Post
How to find all lines in a file that contains a particular word ? For example...let's say the word "from".
other ways:
1) just bash shell
Code:
while read -r line
do
 case "$line" in 
    *from* ) echo "found";;     
 esac
done <"file"
2) awk
Code:
$ awk '/from/' file
3) Python
Code:
for line in open("file"):
  if "from" in line: 
     print line.strip()
4) Perl
Code:
$ perl -ne 'print if /from/;' file
 
  


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
print second word in 1st line along with 5th word in all the lines after the first bangaram Programming 5 08-31-2009 04:42 AM
[SOLVED] find a word in a file, and change a word beneath it ?? vikas027 Programming 10 02-14-2008 10:46 PM
Loop through lines in file to find specified substring dresch Linux - Newbie 16 01-06-2007 06:34 PM
[c shell] How do I find how many lines a file has? saiz66 Programming 5 10-08-2004 04:01 PM
How 2 find a duplicate word in a text file cowardnewbie Programming 1 09-17-2001 12:57 AM


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