You are on page 1of 3

Bash solutions to Chapter 3: ”Bash and python”

1
Code

cat ../resit1Data/genes.fna | grep -v \> | while read line; \


do echo $line | tr -d "\n" | wc -c; done | sort -n | tail -1 > 1.out

2
Code

cat ../resit1Data/genes.fna | grep GGGGTTTGAGCA > 2.out

3
Code

cat ../resit1Data/genes.fna | grep -v \> | cut -b 1-3 | tr acgt ACGT | sort | \


uniq -c | tr -s " " | sort -nr | while read line; do qty=$(echo $line | \
cut -d " " -f 1); codon=$(echo $line | cut -d " " -f 2); \
if [ $qty -ge 5 ]; then echo $qty $codon; fi; done > 3.out

1
3 alternative
If you know AWK, you can get away with this shorter code:
Code

grep -v \> Data/genes.fna | cut -b 1-3 | tr acgt ACGT | sort | uniq -c |\


tr -s " " | sort -nr | awk '{OFS="\t";print $2,$1}'

4
Code

cat ../resit1Data/digit.txt | grep -o . | tr "\n" + | sed "s/+$/\n/" | bc > 4.out

5a
Code

grep -A 1 -f ../resit1Data/id.list ../resit1Data/BjornAndMartin.fastq | \


grep -v -- -- > 5.fastq

5b
Code

grep -A 1 -f ../resit1Data/id.list ../resit1Data/BjornAndMartin.fastq | \


grep -v -- -- | paste - - - - | tr "\t" \| | while read line; do \
seq=$(echo $line | cut -d \| -f 2); gc=$(echo $seq | tr -d atATnN | \
wc -c); allNt=$(echo $seq | tr -d nN | wc -c); gcPercent=$(echo "scale=2;\
$gc/$allNt" | bc ); gcField=$(echo -ne "GC=0$gcPercent"); id=$(echo $line | \
cut -d \| -f 1); qualId=$(echo $line | cut -d \| -f 3); values=$(echo $line | \
cut -d \| -f 4); echo -e "$id $gcField\n$seq\n$qualId\n$values"; done > 5.gc.fastq

2
6
Code

cat poetry.txt | tr '[:upper:]' '[:lower:]' | while read line ; \


do words=( $(echo $line | grep -Eo '\S+' | while read word ; \
do letter=$(echo $word | head -c 1); echo $letter; done)) ; \
for (( i=2; i<${#words[@]};i++)) ; do \
if [ ${words[$i]} = ${words[$i-2]} ]; then echo 1; break; fi; \
done ;done | paste -s -d+ | bc

Backslashes at the end of lines indicate line continuation.

You might also like