首页 > 解决方案 > How to print "\t" in awk

问题描述

I need to read a file, ignore some lines and print the lines I want with their tab delimiters as strings i.e. input line:-

1 7579643 . C <DEL> . . DP=417;DPADJ=1471;RO=417;AO=1054;AF=0.716519374575119;END=7579660;SVLEN=17;SVTYPE=DEL;SVCONF=HIGH . .

output line:-

1\t7579643\t.\tC\t<DEL>\t.\t.\tDP=417;DPADJ=1471;RO=417;AO=1054;AF=0.716519374575119;END=7579660;SVLEN=17;SVTYPE=DEL;SVCONF=HIGH\t.\t.

Can this be achieved in awk?

At the moment I have this:-

awk 'BEGIN{FS="\n"}{gsub(/\t/, /\\t/); if ($1 !~/#/) print $1}' test.vcf

But my output strings not showing the separators correctly:-

1075796430.0C0<DEL>0.0.0DP=417;DPADJ=1471;RO=417;AO=1054;AF=0.716519374575119;END=7579660;SVLEN=17;SVTYPE=DEL;SVCONF=HIGH0.0.

Happy to do it in plain bash or sed etc.

标签: awk

解决方案


Well, you have FS="\n" and I don't understand it in this context and could therefore be off with this one but:

$ awk 'BEGIN{FS="\t";OFS="\\t"}{$1=$1;print}' file

Explained:

awk '
BEGIN {       
    FS="\t"    # tab separated
    OFS="\\t"  # \t string separated
}
{
    $1=$1      # rebuild the record
    print      # output
}' file

(Maybe changing that FS="\n" alone would fix your problem, didn't try that, too many tabs to fix.)


推荐阅读