A file stream is a stream of bytes coming from a file; think of it
as a "Netflix" view of a file. Both stdin
and
stdoout
are file streams. The file stream stdin
is an input file stream and the file stream stdout
is an output file stream.
UNIX filters are commands that create or transform file streams. They have
as input a file stream and as output a file stream. File streams can be
captured into a file; by default they are put to stdout
. All UNIX
filters have these common features.
- They expect an input. The default is
stdin
, but they often get input from a pipe (|). If you pass them a file or a wildcard as an argument, a file stream is created from that file. - Filters "filter in" data from their input to produce their output.
- They produce output. The default is for the output to go to
stdout
(the screen). Their output can be sent to a pipe (|) or to a file using>
or>>
.
The figure below illustrates the action of a UNIX filter.
---------------- input -----------> | FILTER | ----------> output ---------------- This is the input This is the filter's file stream. file output stream. By default, this is By default, this is stdin. stdout. This can also come from This can go to a pipe, a pipe, which makes the in which case it preceding filter's output stream becomes the next this filter's input stream filter's input stream.
Filter Cheat Sheet | ||
---|---|---|
Filter | Option | Action |
cat | This is the
non-filter filter; it shows the entire file. You can give it
one or more files as arguments and they will be placed in a file
stream in seriatum . | |
-n | shows output with line numbers | |
head | This filters in lines at the top of a
file. By default, it shows ten lines. Use head -n
to show the first n lines of a file | |
tail | This filters in lines at the bottom of a
file. By default, it shows ten lines. Use tail -n
to show the last n lines of a file | |
grep | This filters in lines
containing its first argument as a substring. The second argument should be
the file you want to filter. You can rummage through several files in
seriatum if you pass them as arguments. You can specify these files with
a wildcard such as *.java (all files ending with
.java Usage: grep searchString file(s). | |
-c | prints the number of lines found from grep. | |
-i | ignore case | |
-l | prints the file name (only) if grep
makes a match in the file | |
-n | prints line numbers along with lines from file | |
-v | prints all lines NOT containing the search string you are grepping for. | |
-w | searches for a whole word only | |
sort | This sorts the lines that are its input asciicographially, case insensitive. | |
-b | ignore leading blanks | |
-n | sort numerically (use for files with numbers) | |
-r | reverse sorting order | |
uniq | removes neighboring duplicate lines. You almost always want ot sort first. | |
-c | Precede each line with the number of times it was duplicated in the input file. | |
-d | Only output duplicated lines | |
-i | case-insensitive comparison of adjacent lines | |
wc | counts words, lines, and characters of its input | |
-c | count only characters | |
-w | count only words | |
-l | count only lines | |
tr | transforms
strings Two arguments are required, strings of equal length.
Each character the input stream matching a character in the first
string is translated to the corresponding character in the
second string. For example tr abc xyz changes a to x,
b to y, and c to z. You can pass a range such as a-z as an argument.
For example, tr a-z A-Z uppercases all alphas. |
|
-d | When using this option, give tr
a string as an argument and all characters in that string are deleted.
| |
-cd | When using this option, give tr
a string as an argument and all characters not in that string are deleted.
|
Connector Cheat Sheet | ||
---|---|---|
Connector | Usage | Action |
> | command > file | redirects
command 's output to the file file , performing a
destructive overwrite |
>> | command >> file | appends the
output of command the file file |
| | command1 | command2 | redirects the
output of command1 to be the input of
command2 |