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 19:40] – 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 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/ | ||
| + | </ | ||
| **Sed and special characters. ** | **Sed and special characters. ** | ||
| Line 70: | Line 105: | ||
| < | < | ||
| - | sed s/\t// | + | sed s/ |
| - | sed -e ' | + | sed -e ' |
| </ | </ | ||
| - | Combine with regular expressions.-> Try converting the first 3 characters into " | + | <hidden Exercise> |
| + | Combine with regular expressions -> Try converting the first 3 characters into " | ||
| + | </ | ||
| ---- | ---- | ||
| Line 117: | Line 154: | ||
| echo "a b c d e" | rev | 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 | ||
| + | </ | ||
| + | </ | ||
| ---- | ---- | ||
| - | ====== | + | ====== |
| ===== For-loop ===== | ===== For-loop ===== | ||
| Line 135: | Line 227: | ||
| **For-loop** | **For-loop** | ||
| - | * for <fc # | + | <ff serif>< |
| * A <fc # | * A <fc # | ||
| Line 147: | 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 | ||