meta data for this page
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| general:computerenvironment:shell_basic_commands [2019/01/11 15:04] – [Regular expressions] ingo | general:computerenvironment:shell_basic_commands [2020/09/07 18:20] (current) – [If / Else] ingo | ||
|---|---|---|---|
| Line 19: | Line 19: | ||
| Remember to call the file only in the first command given: | Remember to call the file only in the first command given: | ||
| cat ejemplo.txt | grep " | cat ejemplo.txt | grep " | ||
| + | |||
| + | ---- | ||
| + | Tip: You can create this file with: | ||
| + | < | ||
| + | nano ejemplo.txt | ||
| + | </ | ||
| + | paste the contents of the ejemplo file into the nano editor and close it using STRG/CMD + X | ||
| + | |||
| + | Alternatively you can download the file by clicking on its name and do the exercises in a linux environment on your local computer (for example using mobaXterm on windows, or in the command line on macOS). | ||
| + | ---- | ||
| + | |||
| ===== Reading files + counting lines ===== | ===== Reading files + counting lines ===== | ||
| Line 29: | Line 40: | ||
| * '' | * '' | ||
| * '' | * '' | ||
| + | |||
| + | |||
| + | ===== Wildcards ===== | ||
| + | The commands will apply to all the files in the current directory that match the pattern. | ||
| + | " | ||
| + | < | ||
| + | cat *.txt | ||
| + | </ | ||
| + | "?" | ||
| + | < | ||
| + | cat ejemplo.t?t | ||
| + | </ | ||
| + | |||
| + | ---- | ||
| + | |||
| + | ===== Tab completion ===== | ||
| + | |||
| + | You can auto-complete paths by pressing the tab key once or twice anytime while writing a path. If the auto-completion does not work, there might be something wrong with your path.. | ||
| + | |||
| + | You can check out [[https:// | ||
| + | ---- | ||
| ===== Pattern matching using grep ===== | ===== Pattern matching using grep ===== | ||
| Line 45: | Line 77: | ||
| ---- | ---- | ||
| + | |||
| ===== Regular expressions ===== | ===== Regular expressions ===== | ||
| * '' | * '' | ||
| Line 54: | Line 87: | ||
| * '' | * '' | ||
| - | Regular | + | Note that the regular |
| <hidden Exercise> | <hidden Exercise> | ||
| - | Try extracting only the first 3 characters of lines that start with " | + | Regular expressions can be combined with the previous options, particularly with -o to extract characters after a pattern. |
| </ | </ | ||
| Line 62: | Line 95: | ||
| ===== Text editing using sed ===== | ===== Text editing using sed ===== | ||
| - | sed s/x/i/ | + | < |
| - | sed s/x/i/g | + | sed s/ |
| - | sed s/ | + | sed s/ |
| + | sed s/ | ||
| + | </ | ||
| - | Combine with regular expressions. | + | **Sed and special characters. ** |
| - | -> Try converting the first 3 characters into " | + | (An special character might be a space, a tab(\t), a symbol reserved for regular expressions ($), etc.): |
| - | + | ||
| - | sed + special characters | + | |
| - | sed s/\t// | + | |
| - | sed -e ' | + | |
| - | 4) cut | + | < |
| - | cut -f2 | + | sed s/\t// ejemplo.txt |
| - | -> | + | sed -e ' |
| + | </code> | ||
| - | 5) Save/ | + | <hidden Exercise> |
| - | cut -f2 ejemplo.txt | + | Combine with regular expressions |
| + | </ | ||
| - | To save without overwriting (lines get added to file): | + | ---- |
| - | grep " | + | |
| - | 6) both files vs for-loop | + | ===== Cut ===== |
| - | grep " | + | Extract columns from a table |
| - | grep "xxxxx" | + | < |
| - | cat *plo.txt | wc -l | + | cut -f2 ejemplo.txt |
| + | </ | ||
| + | <hidden Exercise> | ||
| + | Extract the first column of lines that contain the character | ||
| + | </ | ||
| + | ---- | ||
| - | for i in *plo.txt; do echo $i; done | + | ===== Save/ |
| - | for i in *plo.txt; do cat $i | wc -l; done | + | (ACHTUNG: do not give same name as input file) |
| + | < | ||
| + | cut -f2 ejemplo.txt > copy_ejemplo.txt | ||
| + | To save without overwriting (lines get added to file): | ||
| + | grep " | ||
| + | </ | ||
| + | ---- | ||
| + | |||
| + | ===== Other commands ===== | ||
| + | |||
| + | **Echo:** | ||
| + | Print something on the terminal | ||
| + | < | ||
| + | echo " | ||
| + | </ | ||
| + | **Translate: | ||
| + | Convert one character to another. | ||
| + | < | ||
| + | cut -f1 ejemplo.txt | ||
| + | cut -f1 ejemplo.txt | tr -d ' | ||
| + | Notice anything in the output? Allow me to fix it: | ||
| + | cut -f1 ejemplo.txt | tr -d ' | ||
| + | **Rev:** | ||
| + | print each line backwards | ||
| + | < | ||
| + | echo "a b c d e" | ||
| + | echo "a b c d e" | rev | ||
| + | </ | ||
| + | |||
| + | ===== Common errors ===== | ||
| + | <hidden Calling the file twice> | ||
| + | < | ||
| + | grep " | ||
| + | </ | ||
| + | The output of the first part of the command is not getting passed as the input to the second part of the command, since the file is being read again. | ||
| + | </ | ||
| + | <hidden Saving file with the input name> | ||
| + | < | ||
| + | cat ejemplo.txt > copy.txt | ||
| + | grep " | ||
| + | cat copy.txt | ||
| + | </ | ||
| + | Input file is also the output. The contents of input file will be brutally deleted. | ||
| + | </ | ||
| + | <hidden Moving files into non-existing directories> | ||
| + | < | ||
| + | cat ejemplo.txt > copy.txt | ||
| + | mv copy.txt folder | ||
| + | cd folder | ||
| + | cat folder | ||
| + | </ | ||
| + | If a directory does not exist, ' | ||
| + | <hidden Grep and quotations> | ||
| + | Trying to find all ">" | ||
| + | <file txt important_sequence.fasta> | ||
| + | > | ||
| + | AAATTCTCACCCCTCAGAAA | ||
| + | > | ||
| + | ACCTCAGAAAAATTCTCACC | ||
| + | </ | ||
| + | < | ||
| + | grep > important_sequence.fasta | ||
| + | </ | ||
| + | The crocodile ">" | ||
| + | </ | ||
| + | <hidden Wildcards or regular expressions> | ||
| + | < | ||
| + | sed -e ' | ||
| + | </ | ||
| + | Note that " | ||
| + | </ | ||
| + | <hidden File names> | ||
| + | Some naming schemes can make your life as a programmer a living hell. | ||
| + | <file txt output file final final definitive 6.fasta> | ||
| + | aaa | ||
| + | bbb | ||
| + | ccc | ||
| + | </ | ||
| + | < | ||
| + | rename -e 's/_/ /g' output_file_final_final_definitive_6.fasta | ||
| + | cat output\ file\ final\ final\ definitive\ 6.fasta | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | ---- | ||
| + | |||
| + | ====== Slightly more advanced bash commands ====== | ||
| + | |||
| + | ===== For-loop ===== | ||
| + | First we will create another file. Observe differences in output when concatenating both files and when working with them separately in a for-loop: | ||
| + | < | ||
| + | grep " | ||
| + | cat ejemplo.txt | wc -l | ||
| + | cat copy_ejemplo.txt | wc -l | ||
| + | </ | ||
| + | |||
| + | **Both files** | ||
| + | < | ||
| + | |||
| + | **For-loop** | ||
| + | |||
| + | <ff serif>< | ||
| + | |||
| + | * A <fc # | ||
| + | * <fc #800080> Instruction for each element of your list</ | ||
| + | * The rest is part of the basic sintax of a for-loop. | ||
| + | |||
| + | We next change the instruction from the example above, to reading the file and counting its lines: | ||
| + | < | ||
| + | |||
| + | Which file is which? | ||
| + | < | ||
| + | |||
| + | <hidden Exercise> | ||
| + | Find the lines that contain " | ||
| + | This means, each file should look like this: | ||
| + | <file txt a.txt> | ||
| + | aaaaa xxxxx | ||
| + | aaaaa bbbbb | ||
| + | axaxa bxbxb | ||
| + | </ | ||
| + | Hint: let me start the command for you: | ||
| + | < | ||
| + | for i in a b c; do ... | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | |||
| + | ---- | ||
| + | |||
| + | ===== AWK ===== | ||
| + | This command is also considered a programming language on its own. It is particularly useful when needing to process the elements of a table. The basic syntax is the following: | ||
| + | |||
| + | <ff serif>< | ||
| + | * Indicate the <fc # | ||
| + | * <fc # | ||
| + | * <fc # | ||
| + | * Columns can also be processed with mathematical operations. For instance, print $1 + $2. | ||
| + | |||
| + | |||
| + | < | ||
| + | grep " | ||
| + | grep " | ||
| + | </ | ||
| + | |||
| + | <ff serif>< | ||
| + | | ||
| + | |||
| + | * You can indicate to only print the rows where the <fc # | ||
| + | * Equal == | ||
| + | * Not equal != | ||
| + | * Greater than > | ||
| + | * <fc # | ||
| + | |||
| + | ---- | ||
| - | 7) translate | + | ===== If / Else ===== |
| - | cat ejemplo.txt | tr -d ' | + | |
| + | < | ||
| + | cat ejemplo.txt | wc -l | ||
| + | if [[ $(cat ejemplo.txt | wc -l) -ge 9 ]] ; then echo "File has nine or more lines"; | ||
| + | </ | ||
| + | To compare numbers: | ||
| + | * -eq = is equal to | ||
| + | * -ne = is not equal to | ||
| + | * -ge = is greater than or equal to | ||
| + | * -le = is lesser than or equal to | ||
| + | * -gt = is greater than | ||
| + | * -lt = is lesser than | ||