awk to split field twice using two deliminators

In the awk I am splitting on the : into array a, then splitting on the - into element b. I can not seem to duplicate b[1] if there is no - after it. Lines 1,2,4 are examples. If there is a - after the number in b[1] then the value to the right of it is $3 in the ouput. Thank you :).

awk '{split($0,a,":"); split(a[2],b,"-");print a[1],b[1],b[2]}' file

file

chr2:29543663
chr1:21900171
chr5:77524067-77524078
chr5:77425028
chr5:77524110-77524118

current

chr2 29543663 
chr1 21900171 
chr5 77524067 77524078
chr5 77425028

desired tab-delimeted

chr2	29543663	29543663
chr1	21900171	21900171
chr5	77524067	77524078
chr5	77425028	77425028
chr5	77524110	77524118

Also tried:
awk

awk -F: '{
split($0,a,"-");
print (a[1],a[2],a[3]);
}'

Hi, what about sed?

sed -r '/-/! s/.*:(.*)/&-\1/;s/[:-]/\t/g' file

--- Post updated at 18:51 ---

awk -F"[:-]" '{print $1 "\t" $2 "\t" ($3?$3:$2)}' file

--- Post updated at 18:53 ---

awk -F"[:-]" '$3=$3?$3:$2' OFS='\t' file
2 Likes

Thank you very much :).