Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 11-14 of 14 total

substitution for 'ps -aux | grep [P]ROCESS'

as we know, invoking grep after ps with first letter of the PROCESS enclosed in brackets [] (like this: ps -aux | grep [P]ROCESS), excludes grep PROCESS from the output. so, instead of typing those brackets manualy every time, we may use the code below:
FIRST=`echo $1 | sed -e 's/^\(.\).*/\1/'`
REST=`echo $1 | sed -e 's/^.\(.*\)/\1/'`
ps -aux | grep "[$FIRST]$REST"


you may use it as separate shell-script -- like this:
#!/bin/sh
FIRST=`echo $1 | sed -e 's/^\(.\).*/\1/'`
REST=`echo $1 | sed -e 's/^.\(.*\)/\1/'`
ps -aux | grep -v "full/path/to/your/script" | grep "[$FIRST]$REST"


or just include it in your .bashrc or similar -- like this:
function psg
{
FIRST=`echo $1 | sed -e 's/^\(.\).*/\1/'`
REST=`echo $1 | sed -e 's/^.\(.*\)/\1/'`
ps -aux | grep "[$FIRST]$REST"
}

Highlight matching string when using grep

Edit /etc/bash.bashrc (on Debian/Ubuntu), /etc/bashrc (on Fedora) or /etc/profile
export GREP_OPTIONS="--colour=auto"

GREP searching in BBEdit

I do some GREP searching in BBEdit at times, and am saving the code I use for it here so I don't have to look it up every time!

The following will find any lines that have text like this: /anynumberoflettersortext/ABC - where ABC is any three letters, uppercase or lowercase, but no more than 3.

/[-a-zA-Z0-9]+?/[-a-zA-Z]{3}

Find Files Containing Text

Find files that contain a text string:

grep -lir "some text" *


The -l switch outputs only the names of files in which the text occurs (instead of each line containing the text), the -i switch ignores the case, and the -r descends into subdirectories.
« Newer Snippets
Older Snippets »
Showing 11-14 of 14 total