Useful one-line shell commands

SED

  • sed “s/FOO/BAR/g” file.txt #substitute FOO to BAR in the file.txt
  • sed “/PATTERN/s/FOO/BAR/g” file.txt #substitute FOO with BAR only lines which contains PATTERN
  • sed “s/red/blue/g;s/yellow/blue/g” file.txt #substitute red or yellow with blue
  • sed “:a;N;s/\n/ /g;ta” file.txt #substitute line breaks to space in the file.txt
  • sed -i “1 i\Header” file.txt #insert line “Header” in the position 1
  • sed -i “/PATTERN/d” file.txt #remove lines with PATTERN

GREP

  • grep “PATTERN” file.txt #find lines with PATTERN in the file.txt
  • grep “PATTERN” -A 1 file.txt #find lines with PATTERN and show 1 line After context
  • grep “PATTERN” -B 1 file.txt #find lines with PATTERN and show 1 line Before

AWK

  • awk “$1>0.5” file.txt #filter file passing only lines where column 1 is greater than 0.5
  • awk {‘print $1,$2’} file.txt #print only column 1 and 2 from the file.txt
  • awk “$1!=$2” file.txt #show only lines where column 1 is different from column 2

Determine whether FASTq data is Phred33 or Phred64

  • head -n50 file.fastq | awk ‘{if(NR%4==0) printf(“%s”,$0);}’ | od -A n -t u1 | awk ‘BEGIN{min=100;max=0;}{for(i=1;i<=NF;i++) {if($i>max) max=$i; if($i<min) min=$i;}}END{if(max<=74 && min<59) print “–phred33”; else if(max>73 && min>=64) print “–phred64”; else if(min>=59 && min<64 && max>73) print “–solexa64”; else print “Unknown score encoding\!”;}’
Advertisements
This entry was posted in Blog and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s