The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 04:23 PM
grep a word and display its column Orbix UNIX for Dummies Questions & Answers 1 12-24-2007 08:32 PM
how to move word by word on command line pbsrinivas UNIX for Dummies Questions & Answers 1 11-23-2007 06:17 AM
to display the word containing all of aeiou in a file. arunsubbhian UNIX for Dummies Questions & Answers 1 09-09-2007 03: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 06:29 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-30-2006
ananthmm ananthmm is offline
Registered User
  
 

Join Date: Aug 2006
Posts: 6
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
  #2 (permalink)  
Old 11-30-2006
ask.jackal ask.jackal is offline
Registered User
  
 

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

John Arackal
  #3 (permalink)  
Old 11-30-2006
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

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

Code:
sed -n -e "s/.*\(search string\).*/\1/p" input.txt
  #4 (permalink)  
Old 06-20-2007
knowledge_gain knowledge_gain is offline
Registered User
  
 

Join Date: Jun 2007
Posts: 6
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
  #5 (permalink)  
Old 06-20-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
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"
  #6 (permalink)  
Old 06-20-2007
knowledge_gain knowledge_gain is offline
Registered User
  
 

Join Date: Jun 2007
Posts: 6
Thanks. The output is same as expected.
  #7 (permalink)  
Old 05-29-2008
siba.s.nayak siba.s.nayak is offline
Registered User
  
 

Join Date: Jun 2007
Posts: 164
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
Sponsored Links
Closed Thread

Bookmarks

Tags
linux, solaris

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:35 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0