grep option

This is a discussion on grep option within the Tech Board forums, part of the Community Boards category; Hello guys, I am new to this tool and I read some manual of it. I am wondering what are ...

  1. #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

  2. #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.

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,383
    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.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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.

  4. #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

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    "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

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  7. #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

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  9. #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

  10. #10
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    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..

  11. #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

  12. #12
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    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.

  13. #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

  14. #14
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    di = do

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

  15. #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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 03:09 AM
  2. Replies: 3
    Last Post: 12-06-2008, 06:54 PM
  3. For some reason I end up with memory leak...
    By RaDeuX in forum C Programming
    Replies: 10
    Last Post: 11-25-2008, 11:43 PM
  4. Binary Search Tree
    By penance in forum C Programming
    Replies: 4
    Last Post: 08-05-2005, 05:35 PM
  5. Grep
    By cfriend in forum Linux Programming
    Replies: 3
    Last Post: 09-17-2004, 05:34 AM

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