• Posts
  • RSS
  • ◂◂RSS
  • Contact

  • Colored grep

    January 19th, 2009
    shell, programming  [html]
    I figured out some cool stuff I can do with colored grep. I often want to, when grepping, see the bit that matched in a different color. I knew grep --color would do this, but I'd wanted to be able to do this with multiple greps and multiple colors. Then I noticed the GREP_COLOR environment variable and the grep --color=always options. So I made the aliases:
    alias grey-grep="GREP_COLOR='1;30' grep --color=always"
    alias red-grep="GREP_COLOR='1;31' grep --color=always"
    alias green-grep="GREP_COLOR='1;32' grep --color=always"
    alias yellow-grep="GREP_COLOR='1;33' grep --color=always"
    alias blue-grep="GREP_COLOR='1;34' grep --color=always"
    alias magenta-grep="GREP_COLOR='1;35' grep --color=always"
    alias cyan-grep="GREP_COLOR='1;36' grep --color=always"
    alias white-grep="GREP_COLOR='1;37' grep --color=always"
    
    These let me do things like:
    user@host /path/to/cwd $ echo hello there | blue_grep ll | yellow_grep ere
    hello there
    The primary use for me is when I want to look for something and don't really know what it is yet. Often this will include wanting to find things that are near each other, but I'm not sure how near. The color helps immensely with visual grepping.

    The main downside to this method is that I have to specify the color. What I really want to have is something where multiple calls to color grep in the same pipe automagically use different colors.

    Update 2012-09-25: If you want to just highlight things in the output, you can run these with a -C 10000 argument. Or define some more commands:

    alias highlight-grey="grey-grep -C 10000"
    alias highlight-red="red-grep -C 10000"
    
    I've found multiple colors too much work, though, and so just have:
    alias highlight="grep --color=always -C 10000"
    

    Comment via: facebook

    Recent posts on blogs I like:

    More on the Deutschlandtakt

    The Deutschlandtakt plans are out now. They cover investment through 2040, but even beforehand, there’s a plan for something like a national integrated timetable by 2030, with trains connecting the major cities every 30 minutes rather than hourly. But the…

    via Pedestrian Observations July 1, 2020

    How do cars fare in crash tests they're not specifically optimized for?

    Any time you have a benchmark that gets taken seriously, some people will start gaming the benchmark. Some famous examples in computing are the CPU benchmark specfp and video game benchmarks. With specfp, Sun managed to increase its score on 179.art (a su…

    via Posts on Dan Luu June 30, 2020

    Quick note on the name of this blog

    When I was 21 a friend introduced me to a volume of poems by the 14th-century Persian poet Hafiz, translated by Daniel Ladinsky. I loved them, and eventually named this blog for one of my favorite ones. At some point I read more and found that Ladinsky’s …

    via The whole sky June 21, 2020

    more     (via openring)


  • Posts
  • RSS
  • ◂◂RSS
  • Contact