Difference between revisions of "Command Line Crib Sheet"
From LMU BioDB 2013
(→sed Search/Replace Directives: Add sed///number option.) |
(→Search Pattern Symbols (a.k.a. regular expression or regex symbols): Add { } notation.) |
||
(2 intermediate revisions by one user not shown) | |||
Line 49: | Line 49: | ||
| exit | | exit | ||
| Log out of your command line session. | | Log out of your command line session. | ||
+ | |- | ||
+ | | '''grep''' | ||
+ | | general regular expression parser | ||
+ | | Identifies input lines that match a certain pattern; the pattern is known as a ''regular expression'' or syntax, and some aspects of this ''regex'' notation are listed in the section below | ||
|- | |- | ||
| '''history''' | | '''history''' | ||
Line 73: | Line 77: | ||
| print working directory | | print working directory | ||
| Displays the directory/folder in which you are working; the command prompt also shows this | | Displays the directory/folder in which you are working; the command prompt also shows this | ||
+ | |- | ||
+ | | '''sed''' | ||
+ | | stream editor | ||
+ | | Searches (like '''grep''') ''and'' replaces matching text with another piece of text; matches are also specified with a ''regex'' (one section below) with some text replacement options listed two sections below | ||
|- | |- | ||
| '''Tab''' key | | '''Tab''' key | ||
Line 119: | Line 127: | ||
| Letter or number (i.e., something that would appear in a ''w''ord) | | Letter or number (i.e., something that would appear in a ''w''ord) | ||
| Matches any letter or number, but not punctuation, spaces, or other symbols | | Matches any letter or number, but not punctuation, spaces, or other symbols | ||
+ | |- | ||
+ | | '''[''<characters>'']''' | ||
+ | | “Multiple choice” match | ||
+ | | Matches any single character between the brackets | ||
+ | |- | ||
+ | | '''{''number''}''' | ||
+ | | Specific number of repetitions | ||
+ | | Matches the preceding pattern for the specific number of times; requires the addition of a '''-r''' option after '''sed''' to indicate the use of this “shortcut” notation | ||
|} | |} | ||
Latest revision as of 16:04, 19 September 2013
[edit] Commands, Special Symbols, and Keyboard Shortcuts
! | exclamation point/bang | Shortcut for repeating a command—!<command number> repeats the command with that number (as listed by the history command); !! repeats the last command |
/ | forward slash | Separates successive folders in a cd command |
< | input redirect/less-than sign | Specifies that the content of a file (to the right of the <) should serve as the input into the command on the left |
> | output redirect/greater-than sign | Specifies that the result of a program (to the left of the <) should be saved into the file on the left |
pipe | becomes the input into the program to the right | |
~, ~username | tilde | Shortcut for a user’s home folder; when immediately followed by a username (no spaces), this represents that user’s home folder |
cat <filename> | concatenate a file (long story) | Display the contents of the given file |
cd <directory or folder> | change directory | Changes the current directory/folder to the one indicated in the command |
cp -i <file to copy> <destination of copy> | copy file | Copies the given file to the given destination; the -i stands for “interactive,” meaning that cp will ask permission if a file at the destination might get replaced |
Ctrl-C | “Panic” key—bail out of the current program and jump back to the command line | |
Ctrl-D | “End input” key—ends a program when using it in “practice” mode (where you type the data that you want it to manipulate) | |
exit | exit | Log out of your command line session. |
grep | general regular expression parser | Identifies input lines that match a certain pattern; the pattern is known as a regular expression or syntax, and some aspects of this regex notation are listed in the section below |
history | history | Lists the commands you have typed so far |
left/right arrow keys | Go backward/forward across current command (allowing you to edit it) | |
logout | logout | Log out of your command line session. |
ls | list files | Display the files in the current directory; adding a -F to the command (i.e., ls -F) adds a symbol to the file listings indicating their type |
mv -i <file to move or rename> <new location or name of the file> | move or rename a file | Move or rename a file; as with cp, the -i is an “interactive” mode that asks permission if something might get replaced |
pwd | print working directory | Displays the directory/folder in which you are working; the command prompt also shows this |
sed | stream editor | Searches (like grep) and replaces matching text with another piece of text; matches are also specified with a regex (one section below) with some text replacement options listed two sections below |
Tab key | autocomplete | Lists possible matching choices if there is more than one, or autofills immediately if there is only one match |
up/down arrow keys | Go back and forth through command history |
[edit] Search Pattern Symbols (a.k.a. regular expression or regex symbols)
Used by grep, sed, and XMLPipeDB Match to indicate what patterns to find.
. | Single-character wildcard | Matches any character |
* | Zero or more | Matches zero or more of the symbol that precedes it |
^ | Beginning of line | Matches the pattern after it only if it appears at the beginning of a line |
$ | End of line | Matches the pattern before it only if it appears at the end of a line |
+ | One or more | Matches one or more of the symbol that precedes it |
\" | Double-quote character | Matches a double-quote in a line; the extra backslash (\) is needed so that this quote is not confused for indicating the end of the search pattern |
\d | Number 0-9 (i.e., digit) | Matches any single-digit number |
\w | Letter or number (i.e., something that would appear in a word) | Matches any letter or number, but not punctuation, spaces, or other symbols |
[<characters>] | “Multiple choice” match | Matches any single character between the brackets |
{number} | Specific number of repetitions | Matches the preceding pattern for the specific number of times; requires the addition of a -r option after sed to indicate the use of this “shortcut” notation |
[edit] sed Search/Replace Directives
"s/<pattern to replace>/<replacement text>/g" | Replaces the given pattern with the given text; if the replacement text contains a &, the matched pattern will be substituted for that symbol |
"y/<original characters>/<replacement characters>/" | Replace the original characters with the given replacements, following the order in which the characters are given (note there is no g at the end of this directive) |
\<special character> | Enables the use of characters that otherwise have special meaning for sed (e.g., ", /) as if they were “regular” characters; can be used for both matching and replacing |
number(s) preceding s or y | Makes sed perform the text replacement only for the matching line numbers; line ranges can be given as two numbers separated by a comma (,) |
number instead of g at the end | Makes sed perform the text replacement only for the “number”-th match in a line (but still does this for every line) |
\n | Represents a line break—when used in the replacement portion of a sed command, this breaks one line into two at the point where the matching text was found |
sed ':a;N;$!ba;s/\n//g' | Highly specialized sed command for combining lines—this puts two lines together |
<line number(s)>D | Deletes the lines given by the line numbers (does not touch the original file; instead, this skips the line in the result that sed produces) |