Hi everyone,

I have 100 folders and in each folder i have multiple files as below (Prefix Aardvark change from folder to folder):

Aardvark_GENES_D.fa.1

Aardvark_GENES_D.fa.2

Aardvark_GENES_D.fa.3

Aardvark_GENES_D.fa.4

Aardvark_GENES_D.fa.5

I want to rename them by removing the last extension and replacing GENES with following name the order of name will remain the same for each folder as below:

Aardvark_ACMSD_D.fa

Aardvark_ARID1B_D.fa

Aardvark_CRYM_D.fa

Aardvark_SMO_D.fa

Aardvark_Tlx3_D.fa

I'm using following code it works well for Aardvark bt not for other folder in which prefix contains multiple words e.g for African_savana_elephant it adds Genes name after african jst like below:

African_ACMSD_Savana

African_ARID1B_Savana

African_CRYM_Savana

African_SMO_Savana

where it shoud be like below: African_savana_elephant_ACMSD_D.fa African_savana_elephant_ARID1B_D.fa African_savana_elephant_SMO_D.fa

Code:

paste  <(ls  *.fa.* | sort -t '.' -k3,3n ) list.txt  | awk  '{split($1,a,/_/); printf("mv %s %s_%s_%s\n",$1,a[1],$2,a[3]);}'  | sed 's/\.[0-9]*$//' mv Aardvark_GENES_D.fa.1 Aardvark_ACMSD_D.fa mv Aardvark_GENES_D.fa.2 Aardvark_ARID1B_D.fa mv Aardvark_GENES_D.fa.3 Aardvark_CRYM_D.fa mv Aardvark_GENES_D.fa.4 Aardvark_SMO_D.fa

I'm nwe in perl World Kindly guide me how to edit the above expression for my desired results??????


In reply to Rename multiple files by removing last extension and editing string by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.