A Quick and Dirty grep tutorial
Basic syntax:
grep "search pattern" filename
example
grep "bill" names.txt <------find all lines containing bill in file names.txt
grep -n "bill" names.txt <--------same as above, but also include line numbers
grep -n "^bill" names.txt <--------find all lines that **begin** with bill
grep -n "bill$" names.txt <--------find all lines that end in bill
grep -n "[bB]ill" names.txt <----------find all lines that contain bill or Bill
grep -n "[0123456789]" names.txt <-------find all lines that contain a digit
grep -n "[0-9]" names.txt <------same as above
grep -n "[0-9][0-9]" names.txt <--------find all lines that contain at least 2 consecutive digits
NOTE: a carat (^) inside a character class negates all elements of the chararcter class. For example
[0-9] matches any digit and [^0-9] matches anything except a digit
grep -n "^[^0-9]" names.txt <----- finds all lines that do not begin with a digit.
Remember - you can pipe output to grep, as in
last |grep
"^smith"
<------this will output all sign-ons to the system for user smith
---------------------------------------------------------------------------------------------------
A Quicker and Dirtier sort tutorial
examples
sort names.txt <----sorts on column 1 by default. Defualt delimiter is space
sort -k 3 names.txt <-----sort on column 3, assumes space is the delimiter
sort -k 3 -n names.txt <-----sorts on column 3, numeric sort, space is delimiter
sort -k 3 -n -t: names.txt <-----sorts on column 3, numeric sort, colon(:) is delimiter
sort -k 3 -n -r names.txt <-----sort on column 3, numeric sort, output in reverse order (descending)
last|sort -k 1 >users.txt <------
store in file users.txt an alphabetical listing of all user sign-ons to
the system
--------------------------------------------------------------------------------------------------
A Tremendously Quickerest and Dirtierest quide to wc
wc (word count) - outputs 3 numbers: number of lines, number of words, number of bytes for the specified file
example:
wc users.txt <------perform wc on the text file users.txt
last|grep
"^smith"|wc tell us how many times
user smith signed onto the system