This is how my input file looks like. I am not able to show the header of the file so I am showing the one with 22_160549_T_C_b37 oneThis is how the output file should look like. This is not mine. But a test data imageI have an imputed dosage for chr 22 file. its content is:

#[1]CHROM       [2]POS  [3]REF  [4]ALT  [5]HG00096_HG00096      [6]HG00097_HG00097      [7]HG00099_HG00099      [8]HG00100_HG00100     
22      16051249        T       C       0.0     1.0     1.0     0.0    0.0     1.0     0.0     0.0     1.0     0.0     

I want to remove [] and remove anything coming after _ from this file as well as instead of separate 22 16051249 T and C i want to write:


So the final file should look like:

Id      HG00096 HG00097 HG00099 HG00100 
22_16051249_T_C_b37     0       1       1       0       1       0       0       1       0       

Can anyone please help me out with this.

  • 1
    $\begingroup$ Is your file tab-separated? $\endgroup$
    – user324810
    Commented Jan 15, 2022 at 21:53
  • 1
    $\begingroup$ It's better to ask here question regarding specific steps you are struggling with. You seem to want 1, rename header (which frankly I don't know why you would not do in a text editor if you have just one file) 2, merge some columns 3, add there "b37", which I am not even sure where it comes from. I am sure 1 you manage on your own, 2 might be sneakier, however it already is answered many times on stack exchange (if you can't find one it's fine, just post it), 3 you got to explain better. It's not clear at all. Here we like specific questions. $\endgroup$ Commented Jan 15, 2022 at 22:03
  • $\begingroup$ This is what i have tried so far. I tried to remove the brackets using this command and it worked. sed -e 's/[[^][]*]//g' And for adding the 22_1605149_T_C I did this: awk '{ID=$1""$2" "$3""$4" " +b37; print ID}' genotype.chr22_1.txt > genotype.chr22_2.txt But it just gives me a separate file with these values and not want I want. I tried to join the columns using R but it doesn't generate a separate column. the command used is: genotype_chr22_1$ID <- paste(genotype_chr22_1$CHROM, genotype_chr22_1$POS,genotype_chr22_1$REF, genotype_chr22_1$ALT, sep = "_") $\endgroup$
    – Rhea Bedi
    Commented Jan 15, 2022 at 22:06
  • $\begingroup$ @RheaBedi I can't really understand the given content of your example. On the first line is the header with 8 columns of which the first 4 will be merged into 1 called Id. On the second line, you have 8 columns, and like the previous, I can understand it will become 22_16051249_T_C_b37. But what about the third line? And how is that in your final file, the first line has 5 columns while the second line has more? $\endgroup$
    – user324810
    Commented Jan 15, 2022 at 22:25
  • $\begingroup$ The third line would continue with the values. The first column represent ID and the rest of the columns are sample IDs which represent HG0096. the file would have 2504 sample IDs. and each first row would represent 22_160549_T_C_b37 and their corresponding values 0,1 or 2 $\endgroup$
    – Rhea Bedi
    Commented Jan 15, 2022 at 23:09

1 Answer 1


Assuming that the third line is the continuation of the second line. Here's a one (long) liner:

cat <( \
      paste -d '\t' \
         <(echo "Id") \
         <(head -1 test.tsv \
      | cut -f5- \
      | sed -e '$s/\[[[:digit:]]\+\]//g; s/_HG[[:digit:]]\+//g') ) \
    <( \
      paste -d '\t' \
        <(awk 'NR>1 {print $1"_"$2"_"$3"_"$4"_b37"}' test.tsv) \
        <(awk 'NR>1 {print $0}' test.tsv | cut -f5- | sed 's/\.[[:digit:]]\+//g')) \
> output.tsv
  • $\begingroup$ Yes, this code worked for my input data. $\endgroup$
    – Rhea Bedi
    Commented Jan 18, 2022 at 18:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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