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.
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:
<font 14px/Courier New,Courier,monospace;;inherit;;inherit>awk</font><font 14px/Courier New,Courier,monospace;;#27ae60;;inherit>-</font> <font inherit/Courier New,Courier,monospace;;#27ae60;;inherit>F</font><font inherit/Courier New,Courier,monospace;;#27ae60;;inherit> “\t”</font> '{print <font inherit/inherit;;#2980b9;;inherit>$2</font> <font inherit/inherit;;#8e44ad;;inherit>“\t”</font> $1}'
- use after reading the contents of your table, i.e. after cat file.txt
- indicate the <font inherit/inherit;;#27ae60;;inherit>delimiter</font> <font inherit/inherit;;#27ae60;;inherit>of your table</font> to tabs in this case (default is space)
- <font inherit/inherit;;#2980b9;;inherit>$ indicates the column</font> <font inherit/inherit;;#2980b9;;inherit>number</font>you want to return
- <font inherit/inherit;;#8e44ad;;inherit>Text added to your</font> <font inherit/inherit;;#8e44ad;;inherit>table</font>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
<font 16px/Courier New,Courier,monospace;;inherit;;inherit>awk -F “\t”</font><font 16px/Courier New,Courier,monospace;;#e74c3c;;inherit>'$3==“+“</font> {print <font inherit/inherit;;#f39c12;;inherit>$0</font>}'
- You can indicate you want only the rows in which a <font inherit/inherit;;#e74c3c;;inherit>column meets certain condition.</font>In this case the column 3 has to be exactly a plus sign.
- Equal ==
- Non equal =!
- > Greater than
- In this example $0 will <font inherit/inherit;;#f39c12;;inherit>print</font> <font inherit/inherit;;#f39c12;;inherit>all columns of the table.</font>
Sort
<font inherit/Courier New,Courier,monospace;;inherit;;inherit>sort -r -g -k2</font>
- -r if you want descending order
- -g if you want to sort by number
- -k sort by a specific column
Sort more
<font inherit/Courier New,Courier,monospace;;inherit;;inherit>sort -u -t ' ' -k1,1</font>
- -u for unique
- -t to select a delimiter as space (default is tab)
- -k1,1 to apply the unique only for the value of the first column, but still keeping the rest of the row.
Translate
<font inherit/Courier New,Courier,monospace;;inherit;;inherit>tr '[ATGC]' '[TACG]' | rev</font>
- 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)
<font inherit/Courier New,Courier,monospace;;inherit;;inherit>sed -n -e '/AAA/,/BBB/ p'</font>
- <font inherit/Arial,Helvetica,sans-serif;;inherit;;inherit>This will find AAA, and keep all the lines in</font><font inherit/Arial,Helvetica,sans-serif;;inherit;;inherit>a</font><font inherit/Arial,Helvetica,sans-serif;;inherit;;inherit>file</font><font inherit/Arial,Helvetica,sans-serif;;inherit;;inherit>until</font><font inherit/Arial,Helvetica,sans-serif;;inherit;;inherit>it</font><font inherit/Arial,Helvetica,sans-serif;;inherit;;inherit>reaches</font>BBB.
- <font inherit/Arial,Helvetica,sans-serif;;inherit;;inherit>Pro tip: use this one to extract a sequence in a multi-line fasta.</font>*