2017-10-18 20 views
0

J'ai un fichier qui ressemble actuellement à ceci, par exemple:Inclure la numérotation séquentielle au texte correspondant à

>ENSOFAS001369_p |design:coreoidea-v1,designer:forthman,probes-locus:ENSOFAS001369,probes-probe:,probes-source:Anoplocnemis_curvipes_contig204 
>ENSOFAS001369_p |design:coreoidea-v1,designer:forthman,probes-locus:ENSOFAS001369,probes-probe:,probes-source:Anoplocnemis_curvipes_contig204 
>ENSOFAS001264_p |design:coreoidea-v1,designer:forthman,probes-locus:ENSOFAS001264,probes-probe:,probes-source:Anoplocnemis_curvipes_contig6359 
>ENSOFAS001264_p |design:coreoidea-v1,designer:forthman,probes-locus:ENSOFAS001264,probes-probe:,probes-source:Anoplocnemis_curvipes_contig6359 
>ENSOFAS001369_p |design:coreoidea-v1,designer:forthman,probes-locus:ENSOFAS001369,probes-probe:,probes-source:Alydus_pilosus_contig594 
>ENSOFAS004569_p |design:coreoidea-v1,designer:forthman,probes-locus:ENSOFAS004569,probes-probe:,probes-source:Alydus_pilosus_contig30341 

En se concentrant sur la partie du texte entre les > et | symboles, je veux ajouter une numérotation séquentielle basée sur correspondant ENSOFAS ID numériques. Autrement dit, je veux profiter de cela et faire ceci:

>ENSOFAS001369_p1 |design:coreoidea-v1,designer:forthman,probes-locus:ENSOFAS001369,probes-probe:,probes-source:Anoplocnemis_curvipes_contig204 
>ENSOFAS001369_p2 |design:coreoidea-v1,designer:forthman,probes-locus:ENSOFAS001369,probes-probe:,probes-source:Anoplocnemis_curvipes_contig204 
>ENSOFAS001264_p1 |design:coreoidea-v1,designer:forthman,probes-locus:ENSOFAS001264,probes-probe:,probes-source:Anoplocnemis_curvipes_contig6359 
>ENSOFAS001264_p2 |design:coreoidea-v1,designer:forthman,probes-locus:ENSOFAS001264,probes-probe:,probes-source:Anoplocnemis_curvipes_contig6359 
>ENSOFAS001369_p3 |design:coreoidea-v1,designer:forthman,probes-locus:ENSOFAS001369,probes-probe:,probes-source:Alydus_pilosus_contig594 
>ENSOFAS004569_p1 |design:coreoidea-v1,designer:forthman,probes-locus:ENSOFAS004569,probes-probe:,probes-source:Alydus_pilosus_contig30341 

Je grep pour la recherche dans un textwrangler (.> ENSOFAS (\ d +) _ p (+) \ r), mais je sais que l'éditeur de texte Je ne peux pas faire ce dont j'ai besoin pour ajouter des nombres après _p. Je pense que la version de macOS linux pourrait pour la partie de recherche est grep -E ">ENSOFAS[0-9]\{6\}_p\s|" mais ne sais pas comment obtenir la numérotation entre _p et l'espace blanc avant le |. Les numéros ENSOFAS correspondants ne sont pas regroupés dans le fichier texte, mais je pourrais utiliser une sorte de tri si nécessaire.

Répondre

0

court awk approche:

awk '{ $1=$1""++a[$1] }1' file 

La sortie:

>ENSOFAS001369_p1 |design:coreoidea-v1,designer:forthman,probes-locus:ENSOFAS001369,probes-probe:,probes-source:Anoplocnemis_curvipes_contig204 
>ENSOFAS001369_p2 |design:coreoidea-v1,designer:forthman,probes-locus:ENSOFAS001369,probes-probe:,probes-source:Anoplocnemis_curvipes_contig204 
>ENSOFAS001264_p1 |design:coreoidea-v1,designer:forthman,probes-locus:ENSOFAS001264,probes-probe:,probes-source:Anoplocnemis_curvipes_contig6359 
>ENSOFAS001264_p2 |design:coreoidea-v1,designer:forthman,probes-locus:ENSOFAS001264,probes-probe:,probes-source:Anoplocnemis_curvipes_contig6359 
>ENSOFAS001369_p3 |design:coreoidea-v1,designer:forthman,probes-locus:ENSOFAS001369,probes-probe:,probes-source:Alydus_pilosus_contig594 
>ENSOFAS004569_p1 |design:coreoidea-v1,designer:forthman,probes-locus:ENSOFAS004569,probes-probe:,probes-source:Alydus_pilosus_contig30341 

L'approche alternative avec awk fonction de sub():

awk '{ sub(/$/,++a[$1],$1) }1' file 
+0

Courte approche awk travaillé parfait! –

0

Si awk est une option dans votre configuration:

$ awk '{cnt[$1]++; $1=$1""cnt[$1]; print}' file 
>ENSOFAS001369_p1 |design:coreoidea-v1,designer:forthman,probes-locus:ENSOFAS001369,probes-probe:,probes-source:Anoplocnemis_curvipes_contig204 
>ENSOFAS001369_p2 |design:coreoidea-v1,designer:forthman,probes-locus:ENSOFAS001369,probes-probe:,probes-source:Anoplocnemis_curvipes_contig204 
>ENSOFAS001264_p1 |design:coreoidea-v1,designer:forthman,probes-locus:ENSOFAS001264,probes-probe:,probes-source:Anoplocnemis_curvipes_contig6359 
>ENSOFAS001264_p2 |design:coreoidea-v1,designer:forthman,probes-locus:ENSOFAS001264,probes-probe:,probes-source:Anoplocnemis_curvipes_contig6359 
>ENSOFAS001369_p3 |design:coreoidea-v1,designer:forthman,probes-locus:ENSOFAS001369,probes-probe:,probes-source:Alydus_pilosus_contig594 
>ENSOFAS004569_p1 |design:coreoidea-v1,designer:forthman,probes-locus:ENSOFAS004569,probes-probe:,probes-source:Alydus_pilosus_contig30341 

Explication: $1 contiendra le premier champ (pour chaque ligne), par exemple >ENSOFAS001369_p. Nous utilisons un tableau associatif cnt pour compter les occurrences de chaque jeton unique de $1, et modifions le champ $1 (sortie précédente) pour inclure le compte courant de l'enregistrement/ligne traité.

Le script awk peut être raccourci, mais il est probablement plus lisible et compréhensible sous cette forme.