0

I have an input like so:

chr1    28374C       T       0.0     PASS    AF=0.730769;AO=19;DP=26;
chr1    29631A       G       0.0     PASS    AF=0.6;AO=6;DP=10;
chr1    39322CAC    ACC      0.0     PASS    AF=0.266667,0.266667;AO=4,4;DP=16; 

I want to take the first AF=[0-9]. I have managed to split the column by ";" but I don't know how to only take the first entry. So I have used awk:

cat file | awk '{split($6,a,";"); print a[1]}' 

which gives:

AF=0.730769
AF=0.6
AF=0.266667,0.266667

but I only want:

AF=0.730769
AF=0.6
AF=0.266667
0
2

Awk's split function takes a regular expression as the separator, so you can use a regular expression that includes both ; and ,:

awk '{split($6,a,/[;,]/); print a[1]}' file

(no need for cat and |). You could also split twice, using distinct separators:

awk '{split($6,a,/;/); split(a[1],b,/,/); print b[1]}' file
0
1
$ awk -F'[[:space:],;]+' '{print $6}' file
AF=0.730769
AF=0.6
AF=0.266667
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.