Extract line from sam file by column
1
0
Entering edit mode
6.7 years ago
zshores • 0

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 • 3.3k views
ADD COMMENT
0
Entering edit mode
6.7 years ago
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.

ADD COMMENT
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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.

ADD REPLY

Login before adding your answer.

Traffic: 2619 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6