LookIntoEast
LookIntoEast

Reputation: 8798

Use sed within awk

See the line below:

1   164184236   DEL00004514;DEL00004533 N   <DEL>   .   PASS    .   GT:GL:GQ:FT:RCL:RC:RCR:CN:DR:DV:RR:RV   .   .   .   .   0/1:-11.985,0,-35.4847:120:PASS:20:18:19:1:0:0:12:6 .   .   .   .   .   .   .   .   .   .   .   .   .   0/1:-6.1941,0,-9.19766:62:PASS:4:3:2:1:0:0:3:3  .   .   .   .   .   .   .   .   .   .   .   .   .   .   0/1:-9.19536,0,-11.6939:92:PASS:4:5:7:1:0:0:4:4 .   .   .   .   .   .   .   .   .   .   .   .

My purpose is: for all column, if it's . then I'd like to change into 0/0, so how can I do it? I'm thinking about introduce sed in awk, for example: awk ‘{if($n= ".") sed 's/\./0\/0/g'}' Exactly how should I do

Upvotes: 2

Views: 1865

Answers (3)

SLePort
SLePort

Reputation: 15461

With sed, for a three-spaces column delimiter :

sed 's/\( \{3\}\)\./\10\/0/g' file

If tab delimited :

sed 's/\t\./\t0\/0/g' file

Output :

1   164184236   DEL00004514;DEL00004533 N   <DEL>   0/0   PASS    0/0   GT:GL:GQ:FT:RCL:RC:RCR:CN:DR:DV:RR:RV   0/0   0/0   0/0   0/0   0/1:-11.985,0,-35.4847:120:PASS:20:18:19:1:0:0:12:6 .   0/0   0/0   0/0   0/0   0/0   0/0   0/0   0/0   0/0   0/0   0/0   0/0   0/1:-6.1941,0,-9.19766:62:PASS:4:3:2:1:0:0:3:3  .   0/0   0/0   0/0   0/0   0/0   0/0   0/0   0/0   0/0   0/0   0/0   0/0   0/0   0/1:-9.19536,0,-11.6939:92:PASS:4:5:7:1:0:0:4:4 .   0/0   0/0   0/0   0/0   0/0   0/0   0/0   0/0   0/0   0/0   0/0

Update :

    sed 's/\t\./\t0\/0/2g' file

Start replacing from second . found, assuming that first five columns won't have a dot value.

Upvotes: 1

Ed Morton
Ed Morton

Reputation: 203209

sed is for simple substitutions on individual lines, that is all. This job is a little more than that given the requirement in your comment not to change the 6th field so the right tool for the job is awk:

$ awk 'BEGIN{FS=OFS="\t"} {for (i=1;i<=NF;i++) if (($i==".") && (i!=6)) $i="0/0"} 1' file
1       164184236       DEL00004514;DEL00004533 N       <DEL>   .       PASS    0/0     GT:GL:GQ:FT:RCL:RC:RCR:CN:DR:DV:RR:RV   0/0   0/0      0/0     0/0     0/1:-11.985,0,-35.4847:120:PASS:20:18:19:1:0:0:12:6     0/0     0/0     0/0     0/0     0/0     0/0     0/0   0/0      0/0     0/0     0/0     0/0     0/0     0/1:-6.1941,0,-9.19766:62:PASS:4:3:2:1:0:0:3:3  0/0     0/0     0/0     0/0     0/0   0/0      0/0     0/0     0/0     0/0     0/0     0/0     0/0     0/0     0/1:-9.19536,0,-11.6939:92:PASS:4:5:7:1:0:0:4:4 0/0     0/0   0/0      0/0     0/0     0/0     0/0     0/0     0/0     0/0     0/0     0/0

Upvotes: 3

Master DJon
Master DJon

Reputation: 1965

Here is the awk command I would use :
awk '{for(i = 1; i <= NF; i++) {if ($(i) == ".") {$(i)="0/0"}} print $0}'

Test :

echo "1   164184236   DEL00004514;DEL00004533 N   <DEL>   .   PASS    .   GT:GL:GQ:FT:RCL:RC:RCR:CN:DR:DV:RR:RV   .   .   .   .   0/1:-11.985,0,-35.4847:120:PASS:20:18:19:1:0:0:12:6 .   .   .   .   .   .   .   .   .   .   .   .   .   0/1:-6.1941,0,-9.19766:62:PASS:4:3:2:1:0:0:3:3  .   .   .   .   .   .   .   .   .   .   .   .   .   .   0/1:-9.19536,0,-11.6939:92:PASS:4:5:7:1:0:0:4:4 .   .   .   .   .   .   .   .   .   .   .   ." | awk '{for(i = 1; i <= NF; i++) {if ($(i) == ".") {$(i)="0/0"}} print $0}'

Gives :

1 164184236 DEL00004514;DEL00004533 N <DEL> 0/0 PASS 0/0 GT:GL:GQ:FT:RCL:RC:RCR:CN:DR:DV:RR:RV 0/0 0/0 0/0 0/0 0/1:-11.985,0,-35.4847:120:PASS:20:18:19:1:0:0:12:6 0/0 0/0 0/0 0/0 0/0 0/0 0/0 0/0 0/0 0/0 0/0 0/0 0/0 0/1:-6.1941,0,-9.19766:62:PASS:4:3:2:1:0:0:3:3 0/0 0/0 0/0 0/0 0/0 0/0 0/0 0/0 0/0 0/0 0/0 0/0 0/0 0/0 0/1:-9.19536,0,-11.6939:92:PASS:4:5:7:1:0:0:4:4 0/0 0/0 0/0 0/0 0/0 0/0 0/0 0/0 0/0 0/0 0/0 0/0

Upvotes: 2

Related Questions