Must know commands

uniq
What is uniq?
uniq(1)                  - report or filter out repeated lines in a file
DESCRIPTION
     The
uniq utility reads the specified input_file comparing adjacent lines,
     and writes a copy of each unique input line to the output_file.  If
     input_file is a single dash (`
-') or absent, the standard input is read.
     If output_file is absent, standard output is used for output.  The second
     and succeeding copies of identical adjacent input lines are not written.
     Repeated lines in the input will not be detected if they are not adja-
     cent, so it may be necessary to sort the files first.
uniq -d
only show lines which occur more then once
uniq -u
only show lines that doesn’t occur more then once
uniq
show the input where the repeated lines are filtered out
uniq -c
show the count of number of times the line occur


sort
What is sort?
sort(1)                  - sort lines of text files
sort -b
ignoring blank characters
sort -d
sorts in alphabetic order (only characters, numbers and blank characters have impact)
sort -f
considers big letters as small and ignores the difference.
sort -n
sort in nummerical order
sort -r
turn the sort order upside down
sort -t
add a field separator after -t
sort -k
sort the field number after -k


wc
What is wc?
wc(1)                    - word, line, character, and byte count
wc
shows count of lines, words and character’s
wc -w
show only amount of words
wc -l
show only amount of lines
wc -c
show the amount of character’s


cat
What is cat?
cat(1)                   - concatenate and print files
cat -b
number the non-blank output lines
cat -n
number the output lines
cat -v
display non-printing characters

cut
What is cut?
cut(1)                   - cut out selected portions of each line of a file
cut -c2
show only the second column
cut -1-3
show only character’s from column 1 to 3
cut -c2-
show only character’s from column 2 to and all the other columns
cut -c-10
show only character’s from columns 1 to 10
cut -f2
show field number 2
cut -d’:’ -f1 /etc/passwd
show field number 1 when the field delimiter is :


grep
What is grep?
grep(1)              - Print lines matching a pattern
grep -i
ignore case sensitive matching
grep -c
show the amount of matched patterns
grep -n
show the line number of matched patterns
grep -l
show a list of files containing the matched patterns




Fortune and Cowsay for OS X / Yosemite

This guide works if you are using bash shell.

To see if you are using bash type the following in your Apple Terminal
env | grep SHELL
The output on the terminal should be
SHELL=/bin/bash


What is Fortune?
fortune(6)               - print a random, hopefully interesting, adage


What is Cowsay?
cowsay                   - print a ASCII cow in terminal to say what you want

What is Fortune and Cowsay combined together?
$ fortune | cowsay
 ______________________________________ 
/ Some scholars are like donkeys, they \
| merely carry a lot of books.         |
|                                      |
\ -- Folk saying                       /
 -------------------------------------- 
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

To make this work in OS X / Yosemite, we need to install Brew.

What is Brew?
brew(1)                  - The missing package manager for OS X

Installing Brew
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Installing Fortune and Cowsay
brew install fortune -y && brew install cowsay -y

Testing Fortune and Cowsay
fortune | cowsay
 ______________________________________ 
/ An apple every eight hours will keep \
\ three doctors away.                  /
 -------------------------------------- 
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

Showing Fortune and Cowsay in every new terminal session
vi ~/.bash_profile

Adding "fortune | cowsay" to "~/.bash_profile"
[[ -s "$HOME/.profile" ]] && source "$HOME/.profile" # Load the default .profile

[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
fortune | cowsay

Type :wq to save the file.

Enjoy!



UPDATE!

You can use a similar program called cowthink.
Cowthink does the same as Cowsay, but in a different manner.
When you install Cowsay, Cowthink will come along with it.

See the difference?

Cowsay
fortune | cowsay
 _______________________________________ 
/ Backward conditioning:                \
|                                       |
| Putting saliva in a dog's mouth in an |
\ attempt to make a bell ring.          /
 --------------------------------------- 
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

Cowthink
fortune | cowthink
 _____________________________________ 
( Excuse me, but didn't I tell you    )
( there's NO HOPE for the survival of )
( OFFSET PRINTING?                    )
 ------------------------------------- 
        o   ^__^
         o  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||


Emacs editor and its usage

C = Ctrl
M = Meta or Alt

Moving in emacs
C - x, C - c
quits the editor
M - >
moves to the end of the file
M - <
moves to the beginning of the file
C - v
moves a screen display forward
M - v
moves a screen display back
C - x  ]
moves a page forward
C - x  [
moves a page back
C - n
moves to next line 
C - p  
moves to previous line
C - a
moves to the beginning of line
C - e
moves to the end of line
M - e
moes to the end of sentence
M - a
moves to the beginning of sentence
C - f
moves forward one character
C - b
moves back one character
M - f
moves forward a word
M - b
moves back a word

Deleting in emacs
Delete
deletes the prior character
C - d
deletes the current character
M - Delete
deletes the prior word
M - d
deletes the next word
C - x Delete
deletes the prior sentence
M - k
deletes the rest of the sentence
C - k
deletes the rest of the current line
C - x u
undone the last editing

Search & Replace
M - x  query-replace
Search & Replace
y - changes the incident of the pattern
n - dont change the incident of the pattern, but search for another one
q - dont change the incident of the pattern. Quit search & replace ( can also use ESC)
! - replaces this incident of the pattern and all the other incidents
C - s
searches forward in the file for the pattern
C - r
searches backwards for the pattern

Help menu
C - h, C - h
help screen
C - h i 
the info directory
C - h t
emacs tutorial

Working with files
C - x i
add a file to the buffer
C - x, C - w
writes the buffer to the given file
C - x, C - s
saves the file
C - x, C - f
opens another file to a new buffer
C - x b
change to another buffer

Vi editor and its usage

:q
quits
:q!
forces to quit without saving 
:w
save
h
left
k
up
j
down
l
right
$
move to the end of the line
0
move to the beginning of the line
Ctrl+f
shows next screendisplay
Ctrl+b
shows the screendisplay before
Ctrl+u
show screen half way up
Ctrl+d
show screen half way down
w
move forward one word
b
move backward one word 
O
open a new line over the one you are at
o
open a new line under the one you are at  
:!command
quickly use unix commands while staying in vi.
!!command
outputs the command inside of vi to current line.
!}command
takes the input from the lines and output to given command
!}fmt
reformats the text
G
goes to the last line
3G
goes to line number 3
vi +17
starts vi from line number 17
vi +/name
starts vi from first found word name
:s/old/new
changes old with new
:s/old/new/g
changes every old with new on the same line
:1,$ s/old/new/g
changes all old in every line with new
!G
uses the given command for all lines (mut place the marker on 1st line)
Ctrl+g
shows the current line number and other info
:/
searches string forward (n repeats search)
:?
searches string backward (SHIFT-n repeats search in oppersite direction)