Question: add a blank line after particular line
0
gravatar for amoltej
4.7 years ago by
amoltej90
Australia
amoltej90 wrote:

Hi, 

I have a file as follows -

Mp1087439_TGAC_V1.1_scaffold_1    exon    51615    51678    gene_id "3_g"; transcript_id "3_t";
Mp1087439_TGAC_V1.1_scaffold_1    exon    53627    53777    gene_id "3_g"; transcript_id "3_t";
Mp1087439_TGAC_V1.1_scaffold_1    exon    56113    56171    gene_id "3_g"; transcript_id "3_t";
Mp1087439_TGAC_V1.1_scaffold_1    exon    61779    61841    gene_id "4_g"; transcript_id "4_t";
Mp1087439_TGAC_V1.1_scaffold_1    exon    61942    62137    gene_id "4_g"; transcript_id "4_t";
Mp1087439_TGAC_V1.1_scaffold_1    exon    62322    62513    gene_id "4_g"; transcript_id "4_t";
Mp1087439_TGAC_V1.1_scaffold_1    exon    62596    62762    gene_id "4_g"; transcript_id "4_t";
Mp1087439_TGAC_V1.1_scaffold_1    exon    63136    63331    gene_id "4_g"; transcript_id "4_t";
Mp1087439_TGAC_V1.1_scaffold_1    exon    73319    73368    gene_id "5_g"; transcript_id "5_t";
Mp1087439_TGAC_V1.1_scaffold_1    exon    75842    76266    gene_id "5_g"; transcript_id "5_t";
Mp1087439_TGAC_V1.1_scaffold_1    exon    76572    76766    gene_id "5_g"; transcript_id "5_t";
Mp1087439_TGAC_V1.1_scaffold_1    exon    77576    77751    gene_id "5_g"; transcript_id "5_t";
Mp1087439_TGAC_V1.1_scaffold_1    exon    103158    103301    gene_id "6_g"; transcript_id "6_t";
Mp1087439_TGAC_V1.1_scaffold_1    exon    103375    103576    gene_id "6_g"; transcript_id "6_t";

 

I want to add a blank line after the gene_id changes. and it should look like - 

Mp1087439_TGAC_V1.1_scaffold_1    exon    51615    51678    gene_id "3_g"; transcript_id "3_t";
Mp1087439_TGAC_V1.1_scaffold_1    exon    53627    53777    gene_id "3_g"; transcript_id "3_t";
Mp1087439_TGAC_V1.1_scaffold_1    exon    56113    56171    gene_id "3_g"; transcript_id "3_t";

Mp1087439_TGAC_V1.1_scaffold_1    exon    61779    61841    gene_id "4_g"; transcript_id "4_t";
Mp1087439_TGAC_V1.1_scaffold_1    exon    61942    62137    gene_id "4_g"; transcript_id "4_t";
Mp1087439_TGAC_V1.1_scaffold_1    exon    62322    62513    gene_id "4_g"; transcript_id "4_t";
Mp1087439_TGAC_V1.1_scaffold_1    exon    62596    62762    gene_id "4_g"; transcript_id "4_t";
Mp1087439_TGAC_V1.1_scaffold_1    exon    63136    63331    gene_id "4_g"; transcript_id "4_t";

Mp1087439_TGAC_V1.1_scaffold_1    exon    73319    73368    gene_id "5_g"; transcript_id "5_t";
Mp1087439_TGAC_V1.1_scaffold_1    exon    75842    76266    gene_id "5_g"; transcript_id "5_t";
Mp1087439_TGAC_V1.1_scaffold_1    exon    76572    76766    gene_id "5_g"; transcript_id "5_t";
Mp1087439_TGAC_V1.1_scaffold_1    exon    77576    77751    gene_id "5_g"; transcript_id "5_t";

Mp1087439_TGAC_V1.1_scaffold_1    exon    103158    103301    gene_id "6_g"; transcript_id "6_t";
Mp1087439_TGAC_V1.1_scaffold_1    exon    103375    103576    gene_id "6_g"; transcript_id "6_t";

 

Can somebody please help me

Thanks 

 

 

/sed /awk /text formatting • 958 views
ADD COMMENTlink modified 4.7 years ago by iraun3.7k • written 4.7 years ago by amoltej90
2
gravatar for iraun
4.7 years ago by
iraun3.7k
Norway
iraun3.7k wrote:

This awk command should work:
 

awk -F'\t' 'NR==1{split($5, a, ";");prevGen=a[1];print}{split($5, a, ";");if (a[1] == prevGen){print}else{print "\n"$0};prevGen=a[1]}' file
ADD COMMENTlink written 4.7 years ago by iraun3.7k
1

Thank you so much. This is very fast than other answer

ADD REPLYlink modified 4.7 years ago • written 4.7 years ago by amoltej90
1
gravatar for 5heikki
4.7 years ago by
5heikki8.6k
Finland
5heikki8.6k wrote:

A very graceless way, assumes a tab-separated file, adds a blank line to the top..

TEST=$(printf "empty"); while IFS=$'\t' read -r NAME TYPE START END GENEID TRANSCRIPTID; do if [ "$TEST" == "$GENEID" ]; then printf "$NAME\t$TYPE\t$START\t$END\t$GENEID\t$TRANSCRIPTID\n"; else printf "\n$NAME\t$TYPE\t$START\t$END\t$GENEID\t$TRANSCRIPTID\n" && TEST=$(printf "$GENEID"); fi; done<inputFile
ADD COMMENTlink modified 4.7 years ago • written 4.7 years ago by 5heikki8.6k
1

Thank you so much 

ADD REPLYlink written 4.7 years ago by amoltej90
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1588 users visited in the last hour