Small Exercises on Shell Variables, cut and grep

ypcat passwd | cut -d: -f5 | grep -w Ho 

ypcat passwd | cut -d: -f5 | grep -w Ho | grep-v " Ho "

*Bourne Shell syntax
FIRST=Dik
LAST=Lee

ypcat passwd | grep -w "$FIRST $LAST"

ALEX="Alex Kean's Project"

ypcat passwd | grep -w "$ALEX" | cut -d: -f1

Pay attention to the use of double quotes to include spaces into a pattern, to hide the effect of single quote. Try

ALEX=Alex Kean's Project
and
grep -w $ALEX
and see why it doesn't work.

Analysis of Word Frequencies in a Passage

Prepared by Tommy Ng

Note: The answer has already assumed the berkeley version of tr.

::::::::::::::
input
::::::::::::::
What is Life?   Perhaps,we will never know what life is!

::::::::::::::
lab6
::::::::::::::
cat input | tr -sc '[A-Z][a-z]' '\012' | tr '[A-Z]' '[a-z]' | \
sort | uniq -c | sort +0r -1

Explanation:
cat input                       # show the content of file 
tr -sc '[A-Z][a-z]' '\012'      # subsitute all non-English characters
                                # to newline character (012)
                                  (assumed /usr/ucb/tr)
tr '[A-Z]' '[a-z]'              # subsitute all upper case letter to lower
case
sort | uniq -c                  # "standard" for counting uniq lines
sort +0 -1 -r                   # sort according to field 0 in reverse
order

::::::::::::::
output
::::::::::::::
   2 is
   2 life
   2 what
   1 know
   1 never
   1 perhaps
   1 we
   1 will

::::::::::::::
reference
::::::::::::::
man tr
......
     -c   Complement  the  set  of  characters  in  string1  with
          respect to the universe of characters whose ASCII codes
          are 01 through 0377 octal;

     -d   Delete all input characters in string1.

     -s   Squeeze all strings of repeated output characters  that
          are in string2 to single characters.
......

man sort
......
     lating sequence is the ASCII character set.  Lines can  also
     be  sorted  according  to the contents of one or more fields
     specified by a  sort-field,  specification,  using  the  +sw
     (starting-word),  -ew  (end-at-word),  and the -tc (set-TAB-
......