Filters

A filter is generally referred to as a program that reads standard input, performs an operation upon it and writes the results to standard output.

 

The following provides examples against some of the more common command line tools.

*example data

 

 

cut

Extracts specified column from each line of a file, based on the following options:

  • -b    byte position
  • -c    character position
  • -f     field position
    • -d    specifies a delimiter (default: tab)

 

 

grep

Extracts lines from files matching a specified pattern. Syntax, grep 'pattern' 'files-to-be-matched:

  • grep 'rossi' *

*useful examples

 

 

head

Extracts first 10 lines of a file by default. Use -n to specify number of lines:

  • head targetFile
  • head targetFile -6

 

 

nl

Number each line within a file:

  • nl targetFile

 

 

paste

Merges multiple files into a single multi-column file:

  • paste targetFile somethingElse

 

 

sort

Sorts input. Default: alphabetically

  • sort targetFile

 

 

split

Splits a file into separate parts:

  • split targetFile
    • default size is 1000 lines

 

 

tac

Reverse of cat:

  • tac targetFile

 

 

tail

Extracts last 10 lines of a file by default. Use -n to specify number of lines:

  • tail targetFile
  • tail targetFile -6

 

 

tee

Used in conjunction with the pipe filter |

Copies the stdin to the specified file(s) and stdout:

  • command | tee targetFile
  • command | tee targetFile1 targetFile2 targetFile3

or, to append:

  • command | tee -a targetFile

 

 

tr

Translates (or deletes) characters

  • tr "[a-z]" "[A-Z]"
    • above translates lower to upper case
    • -d delete pattern
    • -c complements

 

 

uniq

Removes duplicate lines from targetFile:

  • uniq targetFile

 

 

wc

Counts the number of words in a targetFile

  • wc targetFile
    • -c byte count
    • -m character count
    • -l line count
    • -w word count