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/12/01 20:32] – ruben | 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 129: | Line 162: | ||
| 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. | 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. | ||
| </ | </ | ||
| - | < | + | < |
| < | < | ||
| cat ejemplo.txt > copy.txt | cat ejemplo.txt > copy.txt | ||
| Line 137: | Line 170: | ||
| Input file is also the output. The contents of input file will be brutally deleted. | 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> | <hidden Grep and quotations> | ||
| + | Trying to find all ">" | ||
| + | <file txt important_sequence.fasta> | ||
| + | > | ||
| + | AAATTCTCACCCCTCAGAAA | ||
| + | > | ||
| + | ACCTCAGAAAAATTCTCACC | ||
| + | </ | ||
| < | < | ||
| - | echo -e $"> | ||
| - | cat important_sequence.fasta | ||
| grep > important_sequence.fasta | grep > important_sequence.fasta | ||
| </ | </ | ||
| Line 153: | Line 199: | ||
| <hidden File names> | <hidden File names> | ||
| Some naming schemes can make your life as a programmer a living hell. | Some naming schemes can make your life as a programmer a living hell. | ||
| - | <file txt output\ file\ final\ final\ definitive\ 6.fasta> | + | <file txt output file final final definitive 6.fasta> |
| aaa | aaa | ||
| bbb | bbb | ||
| Line 159: | Line 205: | ||
| </ | </ | ||
| < | < | ||
| + | rename -e 's/_/ /g' output_file_final_final_definitive_6.fasta | ||
| cat output\ file\ final\ final\ definitive\ 6.fasta | cat output\ file\ final\ final\ definitive\ 6.fasta | ||
| </ | </ | ||
| </ | </ | ||
| + | |||
| ---- | ---- | ||
| - | ====== | + | ====== |
| ===== For-loop ===== | ===== For-loop ===== | ||
| Line 179: | Line 227: | ||
| **For-loop** | **For-loop** | ||
| - | * for <fc # | + | <ff serif>< |
| * A <fc # | * A <fc # | ||
| Line 191: | Line 239: | ||
| < | < | ||
| + | <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 # | ||
| + | |||
| + | ---- | ||
| + | |||
| + | ===== If / Else ===== | ||
| + | |||
| + | < | ||
| + | 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 | ||