More Grep - Regular Expressions

Thread Tools Search this Thread
# 1  
More Grep - Regular Expressions

Hey all! I'm trying to search a file and return all instances of a word, let's say 'foo' in this case, as long as it's not a function name. For example:

1) int foo; //OK
2) //'this is totally fooed up' is also OK
3) int foo (int x, int y) //not ok to return

I've tried a lot of regular expressions but none of them work. Always is foo the function returned.

Here's what I think should work (but doesn't):

grep -E "foo[\s]*[\w]*[^(]" <filename

Any ideas as to why it doesn't work?
# 2  
Originally Posted by Jombee
Here's what I think should work (but doesn't):

grep -E "foo[\s]*[\w]*[^(]" <filename

Any ideas as to why it doesn't work?
Can you try giving the same command without the '^' sign. That is supposed to match the start of the line.
# 3  
If I take out the ^ then grep matches nothing.

I'm not sure what you mean by it matches the beginning of a line...I thought that the ^ means not.

So basically, what I'm trying to express in a regular way is:

Return to me all the lines that have the string "foo" in them except when there is any amount of whitespace following the word "foo" that is then followed by a "(".

So, what's the deal?
# 4  
grep -E 'foo[^(]+$' filename

# 5  
Wow, that totally worked!

grep -E 'foo[^(]+$' index2.html

Can we go into this a little bit deeper?

-E: match using extended regular expressions.

[^(]: This is confusing me a little bit. Does this mean, "I don't care what comes after the string, as long as it's not a paranthesis?

+: This will happen one or more times

$: end of line?

Thank you so much, guys, for your help!
# 6  
Lets beak the pattern down: 'foo[^(]+$'

'foo' matches the string 'foo' anywhere in the line

'[^(]' matches any character except for a normal open braket '('

'+' means match 1 or more of the preceeding character class,in this case '[^(]'

'$' matches the end of the line

So verbosely, the pattern matches: the string 'foo' anywhere in the line followed by one or more characters to the end of the line as long as none of these characters is a '('.

The only case where this would not match a line you would probably want it to match is the case where the string 'foo' was at the end of a line.
# 7  
If you may have "foo" at the end of the line and still catch it you can use two regexps with the "-e" option:

grep -e "x" -e "y"

will find every "x" and "y".


grep -e "foo[^(][^(]*$" -e "foo$"


Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #928
Difficulty: Medium
64-bit versions of the Unix time stamp will cease to work at 15:30:08 UTC on Sunday, 14 December 2922770265960.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

grep and regular expressions

Hi All, For the past many days I have solved a lot of grep and regular expression questions, Now I am in a search for a good quality set of questions that can help me build and check my knowledge of grep with regular expressions, it would be great if anyone could help me with my requirement. ... (1 Reply)
Discussion started by: rahulkalra9
1 Replies

2. Homework & Coursework Questions

Regular Expressions with GREP

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Given a text file (big_english.txt) containing roughly 250,000 words, answer the following using grep and... (2 Replies)
Discussion started by: blahblahblah123
2 Replies

3. UNIX for Dummies Questions & Answers

extract columns using grep or regular expressions

I am trying to print columns from a table whose name (header) matches a certain string. E.g., patient1001 patient1002 patient2005 patient3005 patient4001 0 0 0 0 0 2 9 2 8 3 2 7 3 0 2 Say I want to print columns whose names end with "01" patient1001 patient4001 0 0 2 3 2 2 ... (3 Replies)
Discussion started by: quextil
3 Replies

4. Shell Programming and Scripting

Regular expressions help

need a regex that matches when a number has a zero (0) at the end of it so like 10 20 120 30 330 1000 and so on (6 Replies)
Discussion started by: linuxkid
6 Replies

5. Shell Programming and Scripting

How to grep using a line break in regular expressions?

Hi, I have a file as below, {#### if file then file else file } print file i need to fine the count of all the pattern - file, inside the { } i'm using a grep command as grep -c \{'*file*'\}\ It doesn't gives me any result, i think the problem here is the... (5 Replies)
Discussion started by: divak
5 Replies

6. Shell Programming and Scripting

searching regular expressions with special characters like dot using grep

hi everybody I am a new user to this forum and its previous posts have been very useful. I'm searching in a file using grep for patterns like 12.13.444 55.44.443 i.e. of form <digit><digit>.<digit><digit>.<digit><digit><digit> Can anybody help me with this. Thanks in advance (4 Replies)
Discussion started by: jpriyank
4 Replies

7. UNIX for Advanced & Expert Users

regular expressions

I have a flat file with the following drug names Nutropin AQ 20mg PEN Cart 2ml Norditropin Cart 15mg/1.5ml I have to extract digits that are before mg i.e 20 and 15 ; how to do this using regular expressions Thanks ram (1 Reply)
Discussion started by: ramky79
1 Replies

8. UNIX for Dummies Questions & Answers

regular expressions

how to find for a file whose name has all characters in uppercase after 'project'? I tried this: find . -name 'project**.pdf' ./projectABC.pdf ./projectABC123.pdf I want only ./projectABC.pdf What is the regular expression that correponds to "all characters are capital"? thanks (8 Replies)
Discussion started by: melanie_pfefer
8 Replies

9. UNIX for Dummies Questions & Answers

grep and regular expressions :

I wrote a simple korn shell where I am trying to filter all the good record layouts of a file to only leave the bad ones to look at. That file is hudge. Aside from '# comments' and 'var=ssss', all record should follow a specific record layout, with comma seperated fields. Some fields can have any... (2 Replies)
Discussion started by: Browser_ice
2 Replies

10. Programming

regular expressions in c++

How do I use the regular expressions in c++? (2 Replies)
Discussion started by: szzz
2 Replies

Featured Tech Videos