LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 01-12-2012, 04:56 AM   #1
newbie0101
Member
 
Registered: Nov 2011
Posts: 47

Rep: Reputation: Disabled
bash print only found words


hello, i have a file that contains a lot of strings.
my goal is to print only words i need.
for example i am searching for part of words ab, cd, ef.
those words might be for example able, abnormal... whatever.
when i use:
Code:
egrep '(ab|cd|ef)' file.txt
it prints whole sentences which contain them but i want get printed only the words, not sentences that contain ab, cd, ef.

how can i do that ?
thanks
 
Old 01-12-2012, 05:24 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Using the -o option prints only the matching part. But.....

In your example using the -o options would only print the ab (cd or ef) part and not the complete word. You need to expand the regular expression to do that. Have a look at this:
Code:
egrep -o '\<[a-z]*(ab|cd|ef)[a-z]*\>' infile
- the \< and \> are word bounderies, they make sure only complete words are matched,
- the [a-z]* parts are there to match possible other parts of the words.

Example:
Code:
$ cat infile
this is not abnormal
coral reef
acda and whatever
an abnormal reef

$ egrep -o '\<[a-z]*(ab|cd|ef)[a-z]*\>' infile 
abnormal
reef
acda
abnormal
reef
Hope this helps.
 
1 members found this post helpful.
Old 01-12-2012, 06:09 AM   #3
newbie0101
Member
 
Registered: Nov 2011
Posts: 47

Original Poster
Rep: Reputation: Disabled
thank you it works ! i just added [a-z,0-9] because i also needed numbers
 
Old 01-12-2012, 07:46 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,
Quote:
Originally Posted by newbie0101 View Post
thank you it works ! i just added [a-z,0-9] because i also needed numbers
[a-z,0-9] should be [a-z0-9] (unless you need to include the comma in the search pattern)

You might want to use [[:alnum:]] instead. This would be the same as: [0-9A-Za-z]

BTW: You're welcome
 
Old 01-13-2012, 02:15 AM   #5
newbie0101
Member
 
Registered: Nov 2011
Posts: 47

Original Poster
Rep: Reputation: Disabled
one more question yet
if the file contains for example more words "able" on the same or different line, how can i print it only once ?
because now i am getting certain words more times
 
Old 01-13-2012, 03:09 AM   #6
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Quote:
Originally Posted by newbie0101 View Post
one more question yet
if the file contains for example more words "able" on the same or different line, how can i print it only once ?
because now i am getting certain words more times
You could pipe it to 'uniq'
 
  


Reply



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
Python: replacing all words found on a list General Programming 3 03-15-2010 08:57 AM
scan binary file for word and print words before and after kpachopoulos Linux - General 5 07-30-2009 10:30 AM
bash alias - two words noir911 Linux - General 4 01-22-2009 09:13 AM
file not found error when using folder names with 2 words sirius57 SUSE / openSUSE 4 08-02-2007 11:07 AM
how many lines and words (bash) sharapchi Programming 4 12-15-2006 12:45 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:11 PM.

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
Open Source Consulting | Domain Registration