Lecture Five

Simple Filter Commands; Redirection; Piping; Multiple Commands

Simple Filter Commands

  • head -7 filename - displays first 7 lines, 10 is default
  • tail -11 filename - displays last 11 lines, 10 is default
  • cut is used to extract fields and characters from records
    • cut -f2 filename - extract 2nd field from all records in file1, using tab as delimiter (default)
    • cut -d' ' -f2,5 filename - extract 2nd and 5th field, using space as delimiter
    • cut -d' ' -f1-3,5 filename - extract 1st through 3rd and 5th fields, using space as delimiter
    • cut -c3-5 filename - extract 3rd to 5th characters
  • sort filename - displays records in ascending order
    • by default uses dictionary (ascii sort) order, from first to last character in each record
    • sort -f filename - sort ignoring case (fold to uppercase)
    • sort -k 3 filename - sort on 3rd field (default field delimiter is space)
    • sort -t: -k 3 filename - sort on 3rd field using colon as field delimiter
    • sort -rk 3 filename - sort on 3rd field in reverse (descending) order
    • sort -nk 5 filename - sort numerically on 5th field
    • sort -u filename - sort records, drop duplicate records
  • tr is used to translate characters to different characters
    • tr a A < filename - translate all characters "a" to "A"
    • tr "[a-z]" "[A-Z]" < filename - translate lowercase "a" through "z" to uppercase
    • tr ':' ' ' < filename - translate all colons to spaces
    • tr ' ' '\n' < filename - translate all spaces to newline characters
    • tr -d '\n' < filename - delete all newline characters
  • wc filename - displays various counts of the contents of a file
    • wc -l filename - displays number of lines in file
    • wc -c filename - displays number of characters in file
    • wc -w filename - displays number of words in file
  • grep 'string' filename - displays lines in file that contain the string
    • grep will be revisited in the lecture about regular expressions

Redirection

Standard Input, Output, and Error

  • standard input, output, and error for commands are sent to your terminal, unless they are redirected
    • cat - (by itself) will take input from terminal (use control-d to end input), send output and error messages to terminal
  • will redirect standard output to a file, deleting any existing contents of the file

    • cat > temp - will redirect output to a file called temp (this technique is sometimes called an "on the fly" document)
  • will redirect standard output to a file, but will append (add to) the end of the file

    • cat temp >> temp2 will append the contents of temp to the contents of temp2
  • 2> will redirect standard error to a file (note that > is the same as 1>)
  • 2>> will append standard error to a file
  • &2 (or 1>&2) will redirect standard output to standard error

  • /dev/null can be used to get rid of unwanted output
    • find / -name *.tmp 2>/dev/null
  • < will redirect standard input from a file, as in previous examples of tr

Piping

  • | (pipe) will connect the standard output of the command to its left, to the standard input of the command to its right
    • ls -al | more
    • ls -al | grep "temp" | sort -rnk5
  • tee will take standard input from a pipe, and send it as output to one or more files and to its standard output
    • ls -al | tee file1 file2

Multiple Commands

  • besides piping, there are other ways that multiple commands may be placed in one line
  • commands may be separated by semi-colons
    • each command will be executed when the previous command has terminated
    • for example: sleep 5; ls
  • commands may be grouped by using parentheses
    • (echo "Who is on:"; w) > whoson
  • commands may also be split over multiple lines, making it easier (for humans) to interpret a long command

    • quote or "escape" the newline character at the end of a line, to get rid of the special meaning of newline (to end a command line)
    • for example:

      echo "This will be split over multiple \ lines. Note that the shell will realize \ that a pipe requires another command, so \ it will automatically go to the next line" | tr '[a-z]' '[A-Z]'