.. include:: ../header.txt .. _filtHoriz: Filtering Horizontally ------------------------ :title:`Linux for Programmers and Users`, Section 4.1 Commands that filter data **horizontally** pass a subset of the **lines** processed to standard output. .. _grep: grep ^^^^ .. index:: grep .. program:: grep .. describe:: grep Print selected parts of lines from each FILE to standard output. The selection is based on matching of PATTERN in the line of input data. :command:`grep` can also read data from stdin (usually via a pipe). SYNOPSIS grep [OPTIONS] PATTERN [FILE...] .. option:: -i, --ignore-case Ignore case distinctions in both the PATTERN and the input files. .. option:: -v, --invert-match Invert the sense of matching, to select non-matching lines. egrep, fgrep ^^^^^^^^^^^^^ .. index:: egrep, fgrep .. describe:: fgrep Fixed string only .. describe:: grep Regular expressions .. describe:: egrep Extended regular expressions .. _uniq: uniq ^^^^^ .. index:: uniq .. program:: uniq .. describe:: uniq Remove repeating lines. The :command:`uniq` command is often used in combination with :ref:`sort` to remove duplicate items. SYNOPSIS uniq [OPTIONS] [INPUT [OUTPUT]] .. option:: -f, --skip-fields=N avoid comparing the first N fields. A field is a run of whitespace, then non-whitespace characters. :: $ cat sample This is a test file for the uniq command. It contains some repeated and some nonrepeated lines. Some of the repeated lines are consecutive, like this. Some of the repeated lines are consecutive, like this. Some of the repeated lines are consecutive, like this. And, some are not consecutive, like the following. Some of the repeated lines are consecutive, like this. The above line, therefore, will not be considered a repeated line by the uniq command, but this will be considered repeated! line by the uniq command, but this will be considered repeated! $ uniq sample This is a test file for the uniq command. It contains some repeated and some nonrepeated lines. Some of the repeated lines are consecutive, like this. And, some are not consecutive, like the following. Some of the repeated lines are consecutive, like this. The above line, therefore, will not be considered a repeated line by the uniq command, but this will be considered repeated! $ .. _sort: sort ^^^^ .. index:: sort .. program:: sort .. describe:: sort sort lines of textual data SYNOPSIS sort [OPTION] [FILE] .. option:: -f, --ignore-case fold lower case to upper case characters .. option:: -g, --general-numeric-sort compare according to general numerical value .. option:: -k, --key=POS1[,POS2] start a key at POS1, end it at POS 2 (origin 1) .. option:: -t, --field-separator=SEP use SEP instead of non-blank to blank transition The following example sorts data from the system's database of users according to their UID (third field) of colon (:) separated fields. Note the data is sort numerically rather than alphabetically by using the -g option. :: $ getent passwd | sort -gt: -k 3,4 awk ^^^^ See :ref:`horiz_awk` .. note:: If awk is to be considered for filtering data, so should perl and python.