This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Extract line from sam file by column

I need to extract a few whole rows from a large sam file, based on column 4. I have been trying to extract it by awk, but to no avail. Anyone able to tell me where I am going wrong?

Looking to extract column 4 with either 76563 or 115625 in it and put it in the output file. The file is of course tab delineated.

samtools view practice.sam | awk -F' ' '$4/76563||115625' output.sam
sam linux awk

1 answer

awk '{OFS="\t"; if($4 == 76563 || $4 == 115625) {print}}' practice.sam > output.sam

Obiously output.sam won't have a header. I'll leave it to you to fix that.

Awesome thanks so much! Why is the print statement needed still/separate from the main statement? Is the OFS an alternate form of -F?

OFS is the output field separator, -F is the input field separator. You can often skip an explicit print, but being able to and it being a good idea are completely unrelated things.

Log in to answer this question.