Pipes
We can send the stanard output of one command to the standard input of another.
“Unix Philosophy”: A program should do one thing, and do it well.
There are many command line utilities that do one thing.
Pipes allow us to connect them together
Probably the most powerful feature of the command line.
$ ls | head
./demos/01-standard_input_and_output.sh
grep
- search inputhead
- print top of inputtail
- print bottom of inputwc
- count inputsort
- sort inputcolumn
- format input into columncut
- pull out column(s) from inputseq
- print sequence of numberspaste
- paste two or more inputs side by sidebc
- command line calculator./demos/02-commands.sh
./demos/03-data_processing.sh
The sandbox directory contains a directory named
log/
.
.txt
are under this directory and all of its sub-directories?The sandbox directory contains a directory named
data/
.
The oscillations.txt
file contains data that
collected using three different detectors. The first column is time (in
seconds), and the next three columns are the values read from detectors
1, 2, and 3.
Write a shell script named count_log_files.sh
that
contains a single pipeline that prints the number of regular files with
a filename that ends with .log
at or below the current
directory.
Write a script named largest_file.sh
that prints out
the name of the file in the current directory that contains the most
lines, and the number of lines it contains.
Write a shell script named max_value_column_3.sh
that takes one argument that specifies a datafile name and prints the
maximum value in column three of the file.
Write a shell script named max_value.sh
that takes
two arguments, the name of a data file and the column number to
consider, and prints the maximum value in the given column for the given
file.
gawk
gawk
is tool for manipulating data in columns.cut
$ gawk '{print $1}' file.txt
$ cat file.txt | gawk '{print $1}'
gawk
gawk script needs to be wrapped in single
quotes (not double quotes){}
are commands. These can be prefixed
with a conditional.$ cat file.txt | gawk '$1 > 10 {print $1}'
'condition {commands...}'
sed
sed
is a stream
editor.cat file.csv | sed 's/,/ /g'
sed
has many more capabilities, but this is by
far the most common use.Let’s ask some questions about the travel rates…
How many places are listed in the table?
What city has the most expensive lodging?
What city has the least expensive lodging?
Where is the greatest lodging meal allowance?
Where is the lowest lodging meal allowance?
How many places in Kansas are there?
Where is the greatest lodging allowance in Kansas?
Where is the lowest lodging allowance in Kansas?
What is the lodging allowance in Hays?
What is the average lodging allowance in Kansas?
What is the average meal allowance in Kansas?
What is the average lodging + meal allowance in Kansas?
What state has the most destinations?
What state has the fewest destinations?
How many different states are there?
This slide intentionally left blank