The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
grep unix.com with google



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 !!

Closed Thread
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-30-2006
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
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,807
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
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,481
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
Registered User
 

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

Join Date: Jun 2007
Posts: 170
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 Off


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 04: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 07:29 AM



All times are GMT -4. The time now is 02:56 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