Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: Why do people say that grep is so powerful?

  1. #1
    Join Date
    Oct 2013
    Beans
    4

    Why do people say that grep is so powerful?

    The only thing I use grep for is to weed out results I don't want returned from a command. I have read tonnes of places that grep is 'so powerful' but they don't explain why.
    Just curious, thanks.

  2. #2
    Join Date
    Apr 2011
    Location
    Mystletainn Kick!
    Beans
    6,927
    Distro
    Ubuntu

    Re: Why do people say that grep is so powerful?

    read what it can do
    Code:
    man grep
    Splat Double Splat Triple Splat
    Earn Your Keep
    Don't mind me, I'm only passing through.
    Once in a blue moon, I'm actually helpful
    .

  3. #3
    Join Date
    Mar 2009
    Beans
    1,634

    Re: Why do people say that grep is so powerful?

    The man page has a tiny fraction of it. To really understand you have to learn regular expressions. Don't just stop at a couple tutorials, go through the entire reference. Grep and egrep can match to just about any character data, and more importantly it can match to a string which is NEAR some other string.

    If you want a good example of a crazy regex, look up 'email regex' and see what you get.

  4. #4
    Join Date
    Dec 2010
    Beans
    Hidden!

    Re: Why do people say that grep is so powerful?

    Thread moved to The Cafe.

    Not an Ubuntu support question.


    404

  5. #5
    Join Date
    Oct 2009
    Location
    Massachusetts, US
    Beans
    35

    Re: Why do people say that grep is so powerful?

    It's great when your programming/scripting. That's where its true power lies, in my opinion. You can parse data with it and extract
    fields and values that you are interested in.

  6. #6
    Join Date
    Nov 2011
    Beans
    2,336
    Distro
    Ubuntu

    Re: Why do people say that grep is so powerful?

    Regular expressions are leveraged throughout the standard toolset in any Unix/Linux/BSD/OS X: In the shell, in grep, in awk, perl, etc., etc., etc.

    Certainly not the most intuitive things to grasp, but knowing even a little bit is very useful.

  7. #7
    Join Date
    Dec 2007
    Beans
    9,025
    Distro
    Lubuntu 16.04 Xenial Xerus

    Re: Why do people say that grep is so powerful?

    Shortened urls: See point #11 here. And clicking on them could be risky.
    Use ubuntu-support-status with caution.

  8. #8
    Join Date
    Mar 2010
    Location
    India
    Beans
    8,175
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Why do people say that grep is so powerful?

    Quote Originally Posted by 1clue View Post
    Grep and egrep can match to just about any character data, and more importantly it can match to a string which is NEAR some other string..
    ..and probably Most importantly, it is super fast at what it does.
    Varun
    Help others by marking threads as [SOLVED], if they are. (See how)
    Wireless Script | Use Code Tags

  9. #9
    Join Date
    Mar 2009
    Beans
    1,634

    Re: Why do people say that grep is so powerful?

    Quote Originally Posted by buzzingrobot View Post
    Regular expressions are leveraged throughout the standard toolset in any Unix/Linux/BSD/OS X: In the shell, in grep, in awk, perl, etc., etc., etc.

    Certainly not the most intuitive things to grasp, but knowing even a little bit is very useful.
    I find this sentiment all over. I can't exactly say it's wrong, but I think it's misleading and tends to make beginners avoid regex at all cost. That's a disservice to beginners who might otherwise tackle it as the difficult but achievable challenge that it is.

    The first impression of regex (regular expressions) is gobbledygook. The beginning learning curve is steep and highly technical.

    Once you get past a certain point however, it DOES start to make sense. There is a structure to it, and at that point it becomes intuitive.

    Let's be completely truthful here: Regular expressions allow extremely precise pattern matching for complex situations. They're very fast, very exact and very compact. Unfortunately they are not very universal. There are at least a dozen flavors of regex that I know of, and they're all in pretty widespread use in different situations. Not all of these are in UN*X though.

    Fortunately enough, the UN*X-oriented regex is pretty consistent. The flavor that egrep uses is an extension of what grep uses, for example. That same regex library is used all over in Linux. But if you go to Java you have to make adjustments. If you go to a lot of programming editors, you can sometimes get more than one flavor of regex in the same editor.

    If you REALLY want to take advantage of UN*X then you need to have at least passable regex skills. Otherwise you'd just as well run Windows. There's a big pot of gold at the end of this rainbow, and you can get some of the payoff before you reach the end of the trail.

    Back when I learned it, in order to really learn it you went out and bought a book. O'Reilly's "Regular Expressions" book was wonderful, I've bought 3 editions because like anything else that's used frequently, regex changes over time. Now I think you can find most of the material online if you're persistent.

  10. #10
    Join Date
    Apr 2011
    Location
    Maryland
    Beans
    1,461
    Distro
    Kubuntu 12.04 Precise Pangolin

    Re: Why do people say that grep is so powerful?

    Quote Originally Posted by 1clue View Post
    I find this sentiment all over. I can't exactly say it's wrong, but I think it's misleading and tends to make beginners avoid regex at all cost. That's a disservice to beginners who might otherwise tackle it as the difficult but achievable challenge that it is.

    The first impression of regex (regular expressions) is gobbledygook. The beginning learning curve is steep and highly technical.

    Once you get past a certain point however, it DOES start to make sense. There is a structure to it, and at that point it becomes intuitive.

    Let's be completely truthful here: Regular expressions allow extremely precise pattern matching for complex situations. They're very fast, very exact and very compact. Unfortunately they are not very universal. There are at least a dozen flavors of regex that I know of, and they're all in pretty widespread use in different situations. Not all of these are in UN*X though.

    Fortunately enough, the UN*X-oriented regex is pretty consistent. The flavor that egrep uses is an extension of what grep uses, for example. That same regex library is used all over in Linux. But if you go to Java you have to make adjustments. If you go to a lot of programming editors, you can sometimes get more than one flavor of regex in the same editor.

    If you REALLY want to take advantage of UN*X then you need to have at least passable regex skills. Otherwise you'd just as well run Windows. There's a big pot of gold at the end of this rainbow, and you can get some of the payoff before you reach the end of the trail.

    Back when I learned it, in order to really learn it you went out and bought a book. O'Reilly's "Regular Expressions" book was wonderful, I've bought 3 editions because like anything else that's used frequently, regex changes over time. Now I think you can find most of the material online if you're persistent.

    Very nicely said!

    I don't know specifically how 'powerful' grep is. But is certainly very, very useful, and I use it several times a day, all day long - and in other forms, like Perl's grep. Here's one possible use, I like to call the needle in a haystack. I have a directory containing 293 experiments worth of run data,and I want to find out which ones contain a specific element. Grep and a little logic to the rescue (plus GNU parallel because I'm impatient!):

    Code:
    $ time parallel "grep -q "AMPL395582581" {} && echo "Found in: {}"" ::: *lowamps*
    Found in: MCC-178_IX008_lowamps.tsv
    Found in: MCC-181_IX006_lowamps.tsv
    Found in: MCC-187_IX006_lowamps.tsv
    
    
    real    0m0.918s
    user    0m0.352s
    sys     0m0.300s
    Less than a second to tell me which experiment I need to be following up on for that particular sample. Pretty good if you ask me!

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •