C Board  

Go Back   C Board > Community Boards > Tech Board

Closed Thread
 
LinkBack Thread Tools Display Modes
Old 02-10-2008, 02:10 AM   #1
Registered User
 
Join Date: May 2006
Posts: 1,579
grep option

Hello guys,


I am new to this tool and I read some manual of it. I am wondering what are your favorite options to grep or frequently used? Could you share some to me -- a newbie of this tool? :-)

Just want to learn some best practices.


thanks in advance,
George
George2 is offline  
Old 02-10-2008, 11:23 AM   #2
pwns nooblars
 
Join Date: Oct 2005
Location: Portland, Or
Posts: 1,094
You could have just appended this to your previous thread on Grep. Just an observation.
__________________
Enlighten Yourself

I'm back!
Wraithan is offline  
Old 02-10-2008, 11:38 AM   #3
(?<!re)tired
 
Mario F.'s Avatar
 
Join Date: May 2006
Location: Portugal
Posts: 5,661
I don't think I have "favorite options" for grep. I use it very seldom. Last time I did was maybe 4 months ago when I decided to rename a variable name I used on several source files.

In essence, this is mostly how grep is used. It's not a tool you use too often (or your shouldn't use too often), or that you can say you have a favorite expression.
__________________
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.


Mario F. is online now  
Old 02-11-2008, 12:48 AM   #4
Registered User
 
Join Date: May 2006
Posts: 1,579
Thanks Mario,


From some manual I found, grep -r option is useful when we assign a directory parameter.

Quote:
Originally Posted by Mario F. View Post
I don't think I have "favorite options" for grep. I use it very seldom. Last time I did was maybe 4 months ago when I decided to rename a variable name I used on several source files.

In essence, this is mostly how grep is used. It's not a tool you use too often (or your shouldn't use too often), or that you can say you have a favorite expression.

regards,
George
George2 is offline  
Old 02-11-2008, 06:27 AM   #5
Cat without Hat
 
CornedBee's Avatar
 
Join Date: Apr 2003
Posts: 8,492
"Favourite option?

You use what's necessary. If grep without options doesn't do what you need, read the manual and see if there is something.
__________________
All the buzzt!
CornedBee

"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
CornedBee is offline  
Old 02-11-2008, 06:46 AM   #6
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
Some quite useful things are:
"grep -ri something . " - search for "something" in this and all subdirectories, ignoring case.
"grep something file|grep -v someother" - search for something, but only on lines that also has someother in them.
"grep something file|grep -v someother" - search for something, but only on lines that DOESN'T have someother.
"grep ^something file(s)" - search for lines that start with something.

There are millions of other variations, but particularly chaining grep and grep -v can be quite handy for narrowing down a large search to a small one. E.g. you know there is a #define called "something", but you can't find which header file it is in, and "something" is used quite frequently in your code, so use "grep -r something .|grep #define" would show you lines that have something and is a #define.

You can of course write expressions that do this using regular expression syntax, but I find it easier to just chain together many grep's.

I sometimes use the "-5" or some such to see lines AROUND what I find.

But there is no such thing as "my favourite grep command". That's like saying "which size screwdriver is your favourite". Unless it's for burglary purposes [bigger is better here!], it's the one that has the right profile(Philips, Pozi, flat, Torx, etc) and size to fit the screw. Don't matter if I think the Pozi #1 is nicer looking, it won't help me screw in a Pozi #3 screw any better than "grep -v" will find what you were looking for in a file [in fact, the latter will show all lines that DON'T match].

--
Mats
__________________
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp is offline  
Old 02-12-2008, 08:34 AM   #7
Registered User
 
Join Date: May 2006
Posts: 1,579
Thanks Mats,


Great experience sharing!

In the following sample, why not using grep -r "#define something" . directly?

Quote:
Originally Posted by matsp View Post

There are millions of other variations, but particularly chaining grep and grep -v can be quite handy for narrowing down a large search to a small one. E.g. you know there is a #define called "something", but you can't find which header file it is in, and "something" is used quite frequently in your code, so use "grep -r something .|grep #define" would show you lines that have something and is a #define.

--
Mats

regards,
George
George2 is offline  
Old 02-12-2008, 08:45 AM   #8
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
Quote:
Originally Posted by George2 View Post
Thanks Mats,


Great experience sharing!

In the following sample, why not using grep -r "#define something" . directly?




regards,
George
Because for example, you don't know the number of spaces (or if it's a tab), or when it's #define something someother, and you are searching for "someother" may be a good example.

Typically, you get "error -5", and you want to know which NAME error 5 is, so you search for -5 and then the #define.

--
Mats
__________________
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp is offline  
Old 02-12-2008, 08:52 AM   #9
Registered User
 
Join Date: May 2006
Posts: 1,579
Thanks Mats,


Quote:
Originally Posted by matsp View Post
Because for example, you don't know the number of spaces (or if it's a tab), or when it's #define something someother, and you are searching for "someother" may be a good example.

Typically, you get "error -5", and you want to know which NAME error 5 is, so you search for -5 and then the #define.

--
Mats

My question is answered.


regards,
George
George2 is offline  
Old 02-14-2008, 09:00 PM   #10
Fountain of knowledge.
 
Join Date: May 2006
Posts: 662
I don't know about grep but I got quite good at regular expressions, for example to reverse the order of two columns of words in a file it was something like

Code:
:%s/{*[0-9a-Z]} {*[0-9a-Z]}/\\2 \\1/
Or something like that.
That is almost certaintly wrong though as I have not used regular expressions for ages.
But I could do a lot of things then which are rather time consuming to do in a 'modern' editor.

I used to use then to make my programs meet the required 'standard' of programming style or
'convention'. So I would have to change all my variable and function names etc..
esbo is offline  
Old 02-14-2008, 11:40 PM   #11
Registered User
 
Join Date: May 2006
Posts: 1,579
Thanks esbo,


What do you think is the best regular expression tutorial for a newbie, so that I can grasp and understand what you wrote below in a day (at least understand the same level)? :-)

Quote:
Originally Posted by esbo View Post
I don't know about grep but I got quite good at regular expressions, for example to reverse the order of two columns of words in a file it was something like

Code:
:%s/{*[0-9a-Z]} {*[0-9a-Z]}/\\2 \\1/
Or something like that.
That is almost certaintly wrong though as I have not used regular expressions for ages.
But I could do a lot of things then which are rather time consuming to do in a 'modern' editor.

I used to use then to make my programs meet the required 'standard' of programming style or
'convention'. So I would have to change all my variable and function names etc..

regards,
George
George2 is offline  
Old 02-14-2008, 11:56 PM   #12
Fountain of knowledge.
 
Join Date: May 2006
Posts: 662
Quote:
Originally Posted by George2 View Post
Thanks esbo,


What do you think is the best regular expression tutorial for a newbie, so that I can grasp and understand what you wrote below in a day (at least understand the same level)? :-)




regards,
George
Not realy sure George it is really a long time since I used them, hence the example I gave
almost certaintly contains errors.

To change cat to dog would be
Code:
s/cat/dog/
to di more than once on a line
Code:
s/cat/dog/g
on every line
Code:
%s/cat/dog/g
to capture cat and put it after dog
Code:
%s/\{cat\}/dog \1/g
Not sure if the above is correct and the syntax might vary in applications soI guess you wil have to try google

This looks OK
http://www.robelle.com/smugbook/regexpr.html

My examples were for search and relace in an editor. (vi)
The equivilent command in unix is 'sed' I believe.
esbo is offline  
Old 02-15-2008, 12:12 AM   #13
Registered User
 
Join Date: May 2006
Posts: 1,579
Thanks esbo!


Looks great! Your below comments, "di" means?

Quote:
Originally Posted by esbo View Post

to di more than once on a line
Code:
s/cat/dog/g

regards,
George
George2 is offline  
Old 02-15-2008, 12:41 AM   #14
Fountain of knowledge.
 
Join Date: May 2006
Posts: 662
di = do

Typing error, 'i' is next to 'o'!!
esbo is offline  
Old 02-15-2008, 01:59 AM   #15
Registered User
 
Join Date: May 2006
Posts: 1,579
Cool, thanks esbo!


Quote:
Originally Posted by esbo View Post
di = do

Typing error, 'i' is next to 'o'!!

regards,
George
George2 is offline  
Closed Thread

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Unknown memory leak with linked lists... RaDeuX C Programming 6 12-07-2008 04:09 AM
My program isn't inserting a new node in the beginning of the list... RaDeuX C Programming 3 12-06-2008 07:54 PM
For some reason I end up with memory leak... RaDeuX C Programming 10 11-26-2008 12:43 AM
Binary Search Tree penance C Programming 4 08-05-2005 05:35 PM
Grep cfriend Linux Programming 3 09-17-2004 05:34 AM


All times are GMT -6. The time now is 03:19 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22