![]() |
| | #1 |
| Registered User Join Date: May 2006
Posts: 1,579
| grep option 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 |
| | #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. |
| Wraithan is offline |
| | #3 |
| (?<!re)tired 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 |
| | #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:
regards, George | |
| George2 is offline |
| | #5 |
| Cat without Hat 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 |
| | #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 |
| | #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:
regards, George | |
| George2 is offline |
| | #8 | |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Quote:
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 |
| | #9 | |
| Registered User Join Date: May 2006
Posts: 1,579
| Thanks Mats, Quote:
My question is answered. regards, George | |
| George2 is offline |
| | #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/
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 |
| | #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:
regards, George | |
| George2 is offline |
| | #12 | |
| Fountain of knowledge. Join Date: May 2006
Posts: 662
| Quote:
almost certaintly contains errors. To change cat to dog would be Code: s/cat/dog/ Code: s/cat/dog/g Code: %s/cat/dog/g Code: %s/\{cat\}/dog \1/g
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 |
| | #13 |
| Registered User Join Date: May 2006
Posts: 1,579
| |
| George2 is offline |
| | #14 |
| Fountain of knowledge. Join Date: May 2006
Posts: 662
| di = do Typing error, 'i' is next to 'o'!! |
| esbo is offline |
| | #15 |
| Registered User Join Date: May 2006
Posts: 1,579
| |
| George2 is offline |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |