GREP and other Leet Tools
Created Friday 09 January 2009
GENERAL NOTE: can put quotes around pattern if contains spaces
GREP searches INSIDE the given stream/file(s) for the pattern string
grep [options] pattern [stream/file]
EXAMPLES
grep -lir jack csYear3 ~ searches INSIDE all files in the given dir (csYear3) for the string "jack" (-l means output filenames not lines, -i = ignore case, -r = recursive)
grep -i dijkstra /home/jack/essay.txt ~ returns lines of the file essay.txt containing the word dijkstra
LS PIPE TO GREP
ls /path/to/dir | grep -i pattern ~ use grep to filter output of ls (optional /path/to/dir, and -i is ignore case)
ls | grep .avi
OTHER LEET TOOLS: CUT and SED (stream editors)
CUT - removes sections from a stream (could be a file). It does this by taking in some text, and splitting it up on some specified delimiter, into what it calls 'fragments'
cut [options] [stream/file]
SED - Stream EDitor
sed [options] [stream/file]
Main use: sed 's/regex/replacement/g' [stream/file] where s=substitution, g=global (apply to all matches instead of just the first).
Note: 'replacement' can be left blank to delete all matches of 'regex'
LEARN BY EXAMPLE: extract your IP address from the verbose output of the ifconfig command:
ifconfig eth0 | grep 'inet addr' | cut -d ':' -f2 | sed 's/[^(.0-9)]//g'
grep: find the relevant lines (those containing the string 'inet addr')
cut: split using a colon delimeter, and select the second fragment
sed: find everything that is not a numeric digit or a dot, and replace with nothing (i.e. delete it). We put regex in square brackets. carot means not, i.e. match [not (full stops and numbers)]