Skip to content

Instantly share code, notes, and snippets.

@Angles
Last active August 29, 2015 14:02
Show Gist options
  • Save Angles/57b35fb4841f62b184ea to your computer and use it in GitHub Desktop.
Save Angles/57b35fb4841f62b184ea to your computer and use it in GitHub Desktop.
GREP color propagation through pipes

GREP color propagation through pipes on OSX

Straight talk (maybe): GREP color=auto vs. color=always

Simple example: ls the user preferences folder, grep for some filename. I hope I did this correctly. I edit poorly.

grep color=auto is ok on final part of cmd line with no pipes after it.

# simple example, list user pref files, grep for names containing "midi"
ls -la1 $HOME/Library/Preferences/ |/usr/bin/grep --color=auto "midi"

# obligatory script pseudo-example
bla.sh "midi"
ls -la1 $HOME/Library/Preferences/ |/usr/bin/grep --color=auto "$*"

To pipe that color into less, Try color=always and less -R

Note: less -r or -R ??? Possible Mavericks likes less -R but earlier OSX liked less -r, maybe that is some emv stuff i did.*

less -R will display ANSI colors, if you provide some! Grep color=always will push ANSI colors into less -R. (references below). NOTE: I found -R better on Mavericks. And though the man page for less says -R would be better, I recall -R looked like crap on earlier OSX verions, where -r is seemed better.

# again, look for filename with "midi" in this folder
ls -la1 $HOME/Library/Preferences/ |/usr/bin/grep --color=always "midi" |less -R

# obligatory script pseudo-example
bla.sh "midi"
ls -la1 $HOME/Library/Preferences/ |/usr/bin/grep --color=always "$*" |less -R

Reference:

Apparently, if using grep with --auto or color=auto post says "However, when it detects a pipe it disables coloring."

Disclaimer

I play geek if I'm lucky. Read the above superuser references.

This gist is a personal reminder about this particular bash arcana. I forget simple crap like this because so many darn coding languages have quirks. I forget about them. Perhaps because I am not the "TechnoCore" (authors Dan Simmons, et al.) More probable, because I resemble the slow zone (Vernor Vinge book lingo).

So my solace is this: I am not a machine. -- But are you sure?

Any script could spit out this nonsense -- simple code examples and subsequent poorly assembled sophistry. More search bots will see this than any human, So who cares.

In the end, I go with the Dan Simmons's Keats cybrid's (perhaps) apocryphal citation of [Keats] 1, or some [later Yeates] 2, (I do not know for sure), to wit:

Things fall apart;
the centre cannot hold;
Mere anarchy is loosed upon the world,
The blood-dimmed tide is loosed,
and everywhere The ceremony of innocence is drowned;

The best lack all conviction,
while the worst Are full of passionate intensity.

Isn't that profound Who knows, it sounds cool, but states the obvious -- debugging code is the worst. Anarchy in any code is stupid, someone will file a bug report. People will hassle you to fix it. It's no fun. You should have written cleaner code -- obviously with PYTHON no doubt.

Geeks know better. Poets? What do they know of the horrors of Bugzilla, or the euphemistically entitled 'Issue Tracker.' Or of some corner case bug no one cares about, but some bonehead assigned you to fix it. Oh. OH, OH!!

Yes, this is sarcasm -- SARCASM! Frustration of bash!
But the code example should be a useful reminder.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment