meta data for this page
This is an old revision of the document!
A non-comprehensive list of bash commands
I have listed a number of common bash commands that come in handy when working in a shell.
You can try the following basic commands on the example file to understand what they are doing.
Feel free to complement your notes on the compilation of basic commands.
Slightly more advanced bash
AWK
This command is also considered a programming language on its own. It is particularly useful when you need to process the elements of a table. The basic syntax is as follows:
awk -F “\t” '{print $2 “\t” $1} file.txt
- -F to indicate the delimiter of your table as tabs (default is space).
- '{print}' to select with “$” the column number you want in the output.
- Text added to the output must be written inside quotation marks; in this case, the text is just the addition of a new tab.
- You can also process columns by mathematical operations. For instance, '{print $1 + $2}'
awk -F “\t” '$3 > 10 {print $0}'
- You can indicate you want only the rows in which a column meets certain condition. For example, the column 3 requires a value greater than 10.
- Equal ==
- Non equal =!
- > Greater than
- '{print $0}' will print all the columns of the table
Sort
sort -g file.txt
- -r to sort in descending order
- -g to sort by number
- -k1,1 to sort by a specific column, in this example, first column only.
- -u make values unique
- -t ' ' to select a non-default field separator (default is tab, and in this case I changed to a space)
Translate
tr '[ATGC]' '[TACG]' | rev
- This is a trick if you are working on the complementary strand.
- It will convert each “A” into “T” and so on.
- rev will make everything read backwards.
sed (slightly more advanced)
sed -n -e '/AAA/,/BBB/ p' file.txt
- This will find AAA, and keep all the lines in a file until it reaches BBB. Pro tip: use this one to extract a sequence in a multi-line fasta.
- Note that using variables inside a sed command requires double quotation marks “ instead of single '.