The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
find a word in a file, and change a word beneath it ?? vikas027 Shell Programming and Scripting 2 02-13-2008 12:23 PM
grep a word and display its column Orbix UNIX for Dummies Questions & Answers 1 12-24-2007 04:32 PM
how to move word by word on command line pbsrinivas UNIX for Dummies Questions & Answers 1 11-23-2007 02:17 AM
to display the word containing all of aeiou in a file. arunsubbhian UNIX for Dummies Questions & Answers 1 09-09-2007 12:57 PM
Can a shell script pull the first word (or nth word) off each line of a text file? tricky Shell Programming and Scripting 5 08-17-2006 03:29 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-30-2006
Registered User
 

Join Date: Aug 2006
Posts: 3
Stumble this Post!
how to grep for a word and display only the word

Hi,

When we "grep" for a word in a file, it returns the lines containing the word that we searched for. Is there a way to display only the words and not the entire line containing them.

Thanks

Ananth
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 11-30-2006
Registered User
 

Join Date: Jan 2006
Posts: 24
Stumble this Post!
grep -o works on linux. Not sure of the Solaris/HP etc.

John Arackal
Reply With Quote
  #3 (permalink)  
Old 11-30-2006
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,658
Stumble this Post!
Or you could use sed to get a grep -o effect.

Code:
sed -n -e "s/.*\(search string\).*/\1/p" input.txt
Reply With Quote
  #4 (permalink)  
Old 06-19-2007
Registered User
 

Join Date: Jun 2007
Posts: 6
Stumble this Post!
Using grep -o in linux, it gives the list wherever the pattern appears. The file
input_file has "Hello World
Hi Hello Hello Hello
Hello
Bye
"
for eg: grep -o "Hello" input_file
Hello
Hello
Hello
Hello
Hello

sed -n -e "s/.*\(Hello\).*/\1/p" input_file
But in using sed, it prints One Hello per line, becoz of \1.

Is it any other way to print same as in linux.

TIA
Reply With Quote
  #5 (permalink)  
Old 06-20-2007
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,397
Stumble this Post!
Quote:
Originally Posted by knowledge_gain View Post
Using grep -o in linux, it gives the list wherever the pattern appears. The file
input_file has "Hello World
Hi Hello Hello Hello
Hello
Bye
"
for eg: grep -o "Hello" input_file
Hello
Hello
Hello
Hello
Hello

sed -n -e "s/.*\(Hello\).*/\1/p" input_file
But in using sed, it prints One Hello per line, becoz of \1.

Is it any other way to print same as in linux.

TIA
Code:
sed -n -e "s/\(Hello\)/\1\\
/gp" file | sed -n -e "s/.*\(Hello\).*/\1/p"
Reply With Quote
  #6 (permalink)  
Old 06-20-2007
Registered User
 

Join Date: Jun 2007
Posts: 6
Stumble this Post!
Thanks. The output is same as expected.
Reply With Quote
  #7 (permalink)  
Old 05-29-2008
Registered User
 

Join Date: Jun 2007
Posts: 47
Stumble this Post!
It would be better if you use -w option to grep.


Quote:
Originally Posted by ananthmm View Post
Hi,

When we "grep" for a word in a file, it returns the lines containing the word that we searched for. Is there a way to display only the words and not the entire line containing them.

Thanks

Ananth
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
linux, solaris

Thread Tools
Display Modes




All times are GMT -7. The time now is 12:26 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0