ir() { perl -pne 's/(.)(.*)/\[\1]\2/' <<< "$@" ;}

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

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
[f]oo

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

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

These Might Interest You

  • Some commands (such as sed and perl) have options to support in-place editing of files, but many commands do not. This shell function enables any command to change files in place. See the sample output for many examples. The function uses plain sh syntax and works with any POSIX shell or derivative, including zsh and bash. Show Sample Output


    1
    inplace() { eval F=\"\$$#\"; "$@" > "$F".new && mv -f "$F".new "$F"; }
    inof · 2010-04-09 11:36:31 8
  • The regular expression matches patterns of .c, .cpp, .C, .Cpp, .h, .hpp, .H, .Hpp. The matched files are piped through etags to create a TAGS file, useful for emacs. Alternate regex (if you aren't worried about capital .Cpp) is -regex ".*\.c\|.*\.cpp\|.*\.h"


    0
    find . -regex ".*\.[cChH]\(pp\)?" -print | etags -
    chidarste · 2009-02-05 16:27:26 0
  • This shell function takes a single argument, which is used as the base name of the .wav, .timing and .session files created. To create a screencast: screencast test type and talk ... then type 'exit' or to exit the screencast. test.wav will contain the audio from your screencast. test.session will contain text and control characters needed to paint the screen test.timing will contain timing information needed to synch individual keystrokes in test.session with the audio. to play back: aplay test.wav & scriptreplay test.{timing,session} NOTE: because the shell function uses the variable "$!", and bash likes to expand '!' during history expansion, you will need to turn off bash's history before you enter the shell function. This can be achieved using the command set +H


    2
    screencast() { arecord -R 1000 -f cd -t wav $1.wav & RECPID=$!; echo "Starting screencast in new shell. Exit subshell to quit."; script -t 2> $1.timing -a $1.session; kill $RECPID; }
    bartonski · 2011-01-20 14:35:47 0
  • you should choose proper color to make comments invisible.


    -1
    vim -c'highlight Comment ctermfg=white' my.conf
    kev · 2011-10-21 00:53:03 1
  • Running this command turns shell tracing and shell verbose debugging on or off. Not only does it do that, it also uses your terminals builtin method of setting colors to make debugging much easier. It looks at the current shell options contained in the $- special bash variable and that lets this function set the opposite of the current value. So from the shell you could do a: setx; echo "y" | ( cat -t ) | echo "d"; setx and it will turn on debbuggin. This is an amazingly useful function that is perfect to add system-wide by adding it to /etc/profile or /etc/bashrc.. You can run it from the shell, and you can also use it in your shell scripts like my .bash_profile - http://www.askapache.com/linux-unix/bash_profile-functions-advanced-shell.html Show Sample Output


    1
    function setx(){ sed '/[xv]/!Q2' <<< $- && { set +xv; export PS4=">>> "; } || { export PS4="`tput setaf 3`>>> `tput sgr0`"; set -xv; }; }
    AskApache · 2010-02-14 01:25:44 1
  • uses the previous "chr" function and uses it to create the inverse function "ord" by brute force. It's slow, It's inelegant, but it works. I thought I needed ord/chr to do a cartesian cipher in shell script a whie ago, but eventualy I realized I could get fancy with tr and do the same thing...


    0
    ord () { seq 1 127 | while read i; do echo `chr $i` $i; done | grep "^$1 " | cut -c '3-' }
    infinull · 2009-10-16 21:54:01 1

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 · 147 weeks and 2 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 · 145 weeks and 4 days 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?

commandlinefu.com 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.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

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: