1

i have input file like this which have duplicates values, i want to give uniq identifier for each uniq value based on first column (col1) mins duplicate rows have same identifier before decimal point based on duplicate value of col1 , any help guyz , thanks in advance.

    Ca3CNSNP431180  2428    2435    0   TTTATttt    AT-Hook 1
    Ca3CNSNP431179  2429    2437    0   TTATTttat   AT-Hook 1
    Ca3CNSNP431178  2428    2436    0   TTTATttta   AT-Hook 1
    Ca4CNSNP431177  1384    1388    0   ATTGA   NF-YB;NF-YA;NF-YC   1
    Ca4CNSNP431176  1382    1386    0   AGATT   Myb/SANT;MYB;ARR-B  1
    Ca4CNSNP431175  1382    1386    0   AGATT   GATA;tify   1
    Ca4CNSNP431174  1386    1398    0   tgaAATTTtcatt   TCR;CPP 2
    Ca4CNSNP431174  1386    1398    0   tgaAATTTtcatt   TCR;CPP 2
    Ca4CNSNP431172  1383    1395    0   gattgAAATTttc   TCR;CPP 2
    Ca4CNSNP431172  1383    1395    0   gattgAAATTttc   TCR;CPP 2
    Ca3CNSNP430205  3334    3343    0   tATATAtata  AT-Hook 3
    Ca3CNSNP430205  3334    3343    0   tATATAtata  AT-Hook 3
    Ca3CNSNP430205  3334    3343    0   tATATAtata  AT-Hook 3

desired output :

identifier  col1    col2    col3    col4    col5    col6    col7
 000001.1   Ca3CNSNP431180  2428    2435    0   TTTATttt    AT-Hook 1
 000002.1       Ca3CNSNP431179  2429    2437    0   TTATTttat   AT-Hook 1
 000003.1       Ca3CNSNP431178  2428    2436    0   TTTATttta   AT-Hook 1
 000004.1       Ca4CNSNP431177  1384    1388    0   ATTGA   NF-YB;NF-YA;NF-YC   1
 000005.1       Ca4CNSNP431176  1382    1386    0   AGATT   Myb/SANT;MYB;ARR-B  1
 000006.1       Ca4CNSNP431175  1382    1386    0   AGATT   GATA;tify   1
 000007.1       Ca4CNSNP431174  1386    1398    0   tgaAATTTtcatt   TCR;CPP 2
 000007.2       Ca4CNSNP431174  1386    1398    0   tgaAATTTtcatt   TCR;CPP 2
 000008.1       Ca4CNSNP431172  1383    1395    0   gattgAAATTttc   TCR;CPP 2
 000008.2       Ca4CNSNP431172  1383    1395    0   gattgAAATTttc   TCR;CPP 2
 000009.1       Ca3CNSNP430205  3334    3343    0   tATATAtata  AT-Hook 3
 000009.2       Ca3CNSNP430205  3334    3343    0   tATATAtata  AT-Hook 3
 000009.3       Ca3CNSNP430205  3334    3343    0   tATATAtata  AT-Hook 3
  • @RomanPerekhrest, i updated my question. – a.k Nov 21 '17 at 9:08
  • NO , headers are not mandatory . – a.k Nov 21 '17 at 9:09
  • are records already sorted? or could be in arbitrary order? – RomanPerekhrest Nov 21 '17 at 9:10
  • i just put headers for better understanding query , they are not required. – a.k Nov 21 '17 at 9:10
  • yeah records are sorted in reverse order , this the part of actual file only – a.k Nov 21 '17 at 9:13
4

Short awk solution:

awk '{ printf "%06d.%d\t%s\n",(!a[$1]++? ++c:c),a[$1],$0 }' file
  • !a[$1]++ - check for unique value of the 1st field $1(used as index for the array a)

  • ++c - c is a prefix value incremented for each unique entry (before decimal point)

  • a[$1] - suffix value (after decimal point) - points to the number of occurrences of the 1st field value $1

  • %06d.%d - output format specificator, where %06d points to the size of the integer part of the number d (size 6 with leading zeros 06) and .%d - the scale of the number (fractional part)


The output:

000001.1    Ca3CNSNP431180  2428    2435    0   TTTATttt    AT-Hook 1
000002.1    Ca3CNSNP431179  2429    2437    0   TTATTttat   AT-Hook 1
000003.1    Ca3CNSNP431178  2428    2436    0   TTTATttta   AT-Hook 1
000004.1    Ca4CNSNP431177  1384    1388    0   ATTGA   NF-YB;NF-YA;NF-YC   1
000005.1    Ca4CNSNP431176  1382    1386    0   AGATT   Myb/SANT;MYB;ARR-B  1
000006.1    Ca4CNSNP431175  1382    1386    0   AGATT   GATA;tify   1
000007.1    Ca4CNSNP431174  1386    1398    0   tgaAATTTtcatt   TCR;CPP 2
000007.2    Ca4CNSNP431174  1386    1398    0   tgaAATTTtcatt   TCR;CPP 2
000008.1    Ca4CNSNP431172  1383    1395    0   gattgAAATTttc   TCR;CPP 2
000008.2    Ca4CNSNP431172  1383    1395    0   gattgAAATTttc   TCR;CPP 2
000009.1    Ca3CNSNP430205  3334    3343    0   tATATAtata  AT-Hook 3
000009.2    Ca3CNSNP430205  3334    3343    0   tATATAtata  AT-Hook 3
000009.3    Ca3CNSNP430205  3334    3343    0   tATATAtata  AT-Hook 3
  • thank you so much @RomanPerekhrest , it works well , can u elobrate printf statement a bit. – a.k Nov 21 '17 at 9:46
  • I think a[$1] should be sufficient enough to record the duplicate count number without the need to introduce another variable suf, so awk '{if(!a[$1]++){++c}; printf "%06d.%d\t%s\n",c,a[$1],$0}' should work too – etopylight Nov 21 '17 at 9:50
  • @etopylight, yes, that's a good hint, updated. Now, it's even more shorter – RomanPerekhrest Nov 21 '17 at 10:00
  • @a.k, welcome, see my update – RomanPerekhrest Nov 21 '17 at 10:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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