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
5
  • @RomanPerekhrest, i updated my question.
    – a.k
    Commented Nov 21, 2017 at 9:08
  • NO , headers are not mandatory .
    – a.k
    Commented Nov 21, 2017 at 9:09
  • are records already sorted? or could be in arbitrary order? Commented Nov 21, 2017 at 9:10
  • i just put headers for better understanding query , they are not required.
    – a.k
    Commented Nov 21, 2017 at 9:10
  • yeah records are sorted in reverse order , this the part of actual file only
    – a.k
    Commented Nov 21, 2017 at 9:13

1 Answer 1

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
4
  • thank you so much @RomanPerekhrest , it works well , can u elobrate printf statement a bit.
    – a.k
    Commented Nov 21, 2017 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
    Commented Nov 21, 2017 at 9:50
  • @etopylight, yes, that's a good hint, updated. Now, it's even more shorter Commented Nov 21, 2017 at 10:00
  • @a.k, welcome, see my update Commented Nov 21, 2017 at 10:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .