This is a test version of Biostars. For the public version, visit https://www.biostars.org.
find and give a number of 4 digits to repeating strings in a column

i have a file with two columns as column1 having a string of multiple characters and column2 having a unique id or value but column can have repeating strings. what i want to do is to give a specific number to repeating strings from 1 to n. like if i have a file like this :

GCF_000009885.1_ASM988v1_protein.faa:WP_000014594.1
GCF_000009885.1_ASM988v1_protein.faa:WP_000025662.1
GCF_920103885.1_DJ_protein.faa:WP_230633553.1
GCF_920103885.1_DJ_protein.faa:WP_230633554.1
GCF_920103885.1_DJ_protein.faa:WP_230633555.1
and so on...

(it's a csv file with columns seperated by : ) what i want to do is :

0001:GCF_000009885.1_ASM988v1_protein.faa:WP_000014594.1
0001:GCF_000009885.1_ASM988v1_protein.faa:WP_000025662.1
0002:GCF_920103885.1_DJ_protein.faa:WP_230633553.1
0002:GCF_920103885.1_DJ_protein.faa:WP_230633554.1
0002:GCF_920103885.1_DJ_protein.faa:WP_230633555.1
and so on...

i'm working in linux, any command that can do this would be helpful !

awk

1 answer

 awk -F ':' '{if(P!=$1) {P=$1;N++;} printf("%04d:%s\n",N,$0);}'  input.txt

thanks it worked!!

Log in to answer this question.