shell function to create an 'invisible regex' that won't show up when grepping the output from 'ps'

ir() { perl -pne 's/(.)(.*)/\[\1]\2/' <<< "$@" ;}
Note that `grep "$(ir foo)"` really doesn't save any typing, but wrapping this inside a second shell function will: psg() { grep "$(ir \"$@\")" ;}
Sample Output
$ ir foo

$ ps aux | grep "$(ir foo)"
[returns nothing]

By: bartonski
2015-07-25 14:13:33

What Others Think

Perl? Ha! I laugh at your perl. Here's ir() using only shell script (shorter and faster :-) ir1() { echo "[${1:0:1}]${1:1} ${@:2}"; } . Take variable $1 (offset 0, 1 character) ${1:0:1} Wrap it in [] [${1:0:1}] Add the rest of variable $1 (offset 1 to the end) ${1:1} . Next show the rest of the parameters ($@ is an array), so this means $2 $3 etc ${@:2}
flatcap · 299 weeks and 4 days ago
or you could just use character class like ps aux | grep [p]rogram it will make the 'p' a regex character class containing only the letter 'p' and will show in ps output as '[p]rogram', but will only search for 'program'
obayemi · 297 weeks and 5 days ago
took me time to read the forum but its actually great. People should know about this more. Cutting
Killersmile · 3 weeks and 1 day ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: