Command Line

Select columns from a file

cut -f 2-5,8 -d , values.csv

"select columns 2 through 5 and columns 8, using comma as the separator". cut uses -f (meaning "fields") to specify columns and -d (meaning "delimiter") to specify the separator.

History

history will print a list of commands you have run recently. Each one is preceded by a serial number to make it easy to re-run particular commands: just type !55 to re-run the 55th command in your history (if you have that many). You can also re-run a command by typing an exclamation mark followed by the command's name, such as !head or !cut, which will re-run the most recent use of that command.

Grep

How can I select lines containing specific values?

head and tail select rows, cut selects columns, and grep selects lines according to what they contain. In its simplest form, grep takes a piece of text followed by one or more filenames and prints all of the lines in those files that contain that text. For example, grep bicuspid seasonal/winter.csv prints lines from winter.csv that contain "bicuspid".

grep can search for patterns as well; we will explore those in the next course. What's more important right now is some of grep's more common flags:

  • -c: print a count of matching lines rather than the lines themselves

  • -h: do not print the names of files when searching multiple files

  • -i: ignore case (e.g., treat "Regression" and "regression" as matches)

  • -l: print the names of files that contain matches, not the matches

  • -n: print line numbers for matching lines

  • -v: invert the match, i.e., only show lines that don't match

Last updated