meta data for this page
This is an old revision of the document!
Basic bash commands
We provide below a set of basic commands to be used in the shell together with an example application. To test the effect of these command, download the file ejemplo.txt below, and give it a try. Note, the example file seems to contain useless information. Still, once you understood what the commands are doing, they will help you during the course to extract and format information from sequence files, even if they contain millions of lines.
- ejemplo.txt
XXXXXXXXXXXXX aaaaa xxxxx xxxxx bbbbb ccccc xxxxx xxxxx ddddd eeeee xxxxx aaaaa bbbbb ddddd fffff axaxa bxbxb XXXXXXXXXXXXX
Play with the following commands. If necessary, make changes to find out what they are doing. Remember to call the file only in the first command given: cat ejemplo.txt | grep “xxxxx”
Reading files + counting lines
cat ejemplo.txtless ejemplo.txt(press q to exit)head -n3 ejemplo.txt(returns the first three lines of the file)tail -n2 ejemplo.txt(returns the last two lines of the file)cat ejemplo.txt | wc -l(counts the number of lines in the file)cat ejemplo.txt | uniq(displays the content of the file by removing successive identical lines)cat ejemplo.txt | sort | uniq(sorts the content of the file, removes successive identical lines, and displays the output)cat ejemplo.txt | sort | uniq -c(counts the number of unique lines in the file)
Pattern matching using grep
grep “xxxxx” ejemplo.txt(search for the pattern 'xxxxx' in the file ejemplo.txt)grep “xxxxx” ejemplo.txt | wc -l(counts the number of lines in ejemplo.txt that contain the pattern 'xxxxx')grep -c “xxxxx” ejemplo.txt(also counts the number of lines in ejemplo.txt that contain the pattern 'xxxxx')grep -v “x” ejemplo.txt(returns the lines that do not contain the pattern)grep -i “x” ejemplo.txt(makes the pattern matching case insensitive)grep -o “aaaaa” ejemplo.txt(returns only the matching pattern)grep -A2 “c” ejemplo.txt(returns the matching plus the next two lines)grep -B2 “c” ejemplo.txt(returns the matching plus the two preceding lines)grep -E “aaaaa|ccccc” ejemplo.txt(returns lines that contain either 'aaaaa' or 'ccccc'
Regular expressions
grep “^x” ejemplo.txt(returns all lines starting with an 'x')grep “x$” ejemplo.txt(returns all lines ending with an 'x')grep “aaaaa…” ejemplo.txt(How many characters are returned?)grep “a*” ejemplo.txt(returns all lines)grep “a.*” ejemplo.txtgrep “a[^a]” ejemplo.txt(returns all lines containing a pattern where at least one 'a' is followed by a different character)grep “a[ax]” ejemplo.txt(returns all lines where an 'a' is either followed by an 'a' or by an 'x')
Regular expressions can be combined with the previous options, particularly with -o to extract characters after a pattern.
Text editing using sed
sed s/x/i/ sed s/x/i/g sed s/axaxa/kkkkk/
Sed and special characters. (An special character might be a space, a tab(\t), a symbol reserved for regular expressions ($), etc.):
sed s/\t// sed -e 's/\t//'
Combine with regular expressions.→ Try converting the first 3 characters into “iii”
Cut
Extract columns from a table
cut -f2 ejemplo.txt
Save/overwrite output
(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 "X" ejemplo.txt >> copy_ejemplo.txt
Other commands
Echo: Print something on the terminal
echo "Witness me"
Translate: Convert one character to another.
cut -f1 ejemplo.txt cut -f1 ejemplo.txt | tr -d '\n' Notice anything in the output? Allow me to fix it: cut -f1 ejemplo.txt | tr -d '\n' | sed -e 's/$/\n/'
Rev: print each line backwards
echo "a b c d e" echo "a b c d e" | rev
Part II
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 "fffff" ejemplo.txt > copy_ejemplo.txt cat ejemplo.txt | wc -l cat copy_ejemplo.txt | wc -l
Both files
cat *plo.txt | wc -l
For-loop
- for i in *plo.txt; do echo $i; done
- A list of files that you want to process; namely, the files ejemplo.txt and copy_ejemplo.txt
- Instruction for each element of your list; in this case, print said element on the terminal. “$i” is each element of list “i”; note that the variable name is specified by the user, so I could create the list “k” and instruct each element “$k”.
- 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:
for i in *plo.txt; do cat $i | wc -l; done
Which file is which?
for i in *plo.txt; do cat $i | wc -l | sed -e "s/^/$i\t/"; done