LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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, 05: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, 06:24 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
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, 07: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, 08:46 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
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, 03: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, 04:09 AM   #6
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,607
Blog Entries: 1

Rep: Reputation: 1047Reputation: 1047Reputation: 1047Reputation: 1047Reputation: 1047Reputation: 1047Reputation: 1047Reputation: 1047
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


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


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