awk split after second underscore in field

I am trying to split a tab-delimeted file using awk after the second _ in bold. The awk below is close but splits on the first

_

, and I am not sure how to use the second _ . Thank you :).

file

chr1    92145889    92149424    NM_001195684_exon_0_10_chr1_92145900_r    0    -
chr1    92161218    92161346    NM_001195684_exon_1_10_chr1_92161229_r    0    -

desired output tab-delimeted

chr1    92145889    92149424    NM_001195684
chr1    92161218    92161346    NM_001195684

awk

awk -F'\t' -v OFS='\t' '{split($4,a,"_"); print $1,$2,$3,a[1]}' file

Oh, come on. :rolleyes: You know how to do this...

awk -F'\t' -v OFS='\t' '{split($4,a,"_"); print $1,$2,$3,a[1]"_"a[2]}' file
1 Like

I know its an easy thing but I couldn't figure it out. I am reading effective awk programming edition 4 , but still have a lot too learn. Thank you very much :).

1 Like

Hello cmccabe,

If your Input_file have string named _exon only in 4th field and not repeating further into any field then you could try following too.

awk '{sub(/_exon.*/,X,$0);print}'  Input_file

If you have multiple strings to be removed from 4th field(2nd underscore) onwards and they are unique and not coming into any other field and you could change above code to following with a minor change too.

awk '{sub(/_exon.*|_etc.*|_another_string.*/,X,$0);print}'   Input_file

Thanks,
R. Singh

1 Like

Thank you all, I appreciate the help :).