LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-03-2011, 02:29 PM   #1
rahularjun86
LQ Newbie
 
Registered: Apr 2011
Location: Frankfurt Am Main, Germany
Distribution: FC14
Posts: 9

Rep: Reputation: 0
Question Grep regular expressions


Hi,
I want to count the number of occurrences of a string "Gene" which is in between one or more spaces in a file using grep. Following is how my file looks like:

Gene 1..2388
CDS 50..2200
tRNA 2709-2790
Gene 3460..4500
CDS 3478..4400

I was trying perl regular expressions using the following command:

$grep -c "^(\w)+Gene(\w)+" file.txt

This is not working I would really appreciate your suggestions...

Last edited by rahularjun86; 12-03-2011 at 02:32 PM.
 
Old 12-03-2011, 02:35 PM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
You can use " | wc -l" to count the number of matching lines.
If "Gene" can appear more than once on a line, you could use the "awk" program, and increment a counter every time one of the fields contains "Gene"
 
1 members found this post helpful.
Old 12-03-2011, 02:44 PM   #3
rahularjun86
LQ Newbie
 
Registered: Apr 2011
Location: Frankfurt Am Main, Germany
Distribution: FC14
Posts: 9

Original Poster
Rep: Reputation: 0
Smile

I solved with the following regex:

grep -ic "[[:space:]]gene[[:space:]]" file.txt

or

grep -i "[[:space:]]gene[[:space:]]" file.txt | wc -l

Do anyone have better idea or regular expression to do the same with grep??? I can do this with perl or other programming languages..

Last edited by rahularjun86; 12-03-2011 at 03:29 PM.
 
Old 12-03-2011, 02:53 PM   #4
corp769
LQ Guru
 
Registered: Apr 2005
Posts: 5,817

Rep: Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002
Honestly, that is up to you and how you want to do it. If you are fine with just using grep, then just use grep
 
Old 12-03-2011, 04:06 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Yes, grep is the standard tool for searching for text strings, so unless you really need to use something else, I doubt it's really worth the extra effort to come up with another solution.

Please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Otherwise html rendering will remove all extra whitespace in front of and between words.

\w is a synonym for [[:alnum:]], a "word" character. What you wanted to use was \s, which is a synonym for [[:space:]].

Also, -c will only count the number of lines matched. If there can be multiple matches on a line, then you may end up under-counting them. You can use grep's -o option, which outputs every match on a separate line, and count the output with wc -l, if this is an issue.

Finally, just to be perfectly clear, the [[:space:]] character class must match an actual character. So the regex you have above will not match the string "gene" if it's at the beginning or end of the line.
 
1 members found this post helpful.
Old 12-03-2011, 05:41 PM   #6
rahularjun86
LQ Newbie
 
Registered: Apr 2011
Location: Frankfurt Am Main, Germany
Distribution: FC14
Posts: 9

Original Poster
Rep: Reputation: 0
Dear David,
Thank you so much for your message, I really appreciate your response,, It is really helpful
Regards,
Rahul
 
  


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
[SOLVED] Regular Expressions and Grep metallica1973 Linux - General 8 03-08-2011 09:17 AM
[SOLVED] Help using wc and grep with regular expressions citygrid Linux - Newbie 12 04-17-2010 03:29 AM
[SOLVED] Why this grep command with regular expressions not working on my system? Andrew Dufresne Linux - Newbie 12 10-01-2009 03:38 PM
\{a,b\} regular expressions sycamorex Linux - General 10 10-18-2008 07:38 PM
Regular expressions using grep linuxmandrake Programming 3 11-16-2005 05:29 PM


All times are GMT -5. The time now is 12:51 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration