Shell script to change files name

Posted on 2006-05-25
Last Modified: 2010-04-21
Hi experts,
I have 500 files with names cctrk_01.sql....cctrk_500.sql. The format of the files content are the same as the one below.

-- Program:   dmp_inbc_s.ppc
-- Function:  dmp_dl_inbc_by_pk
-- Statement: 1 of 1 within function

prompt generating explain plan for statment #10
explain plan set statement_id='cctrk_10' for
WHERE SERVE_SID = :sql_serve_sid AND HOME_SID = :sql_home_sid AND SEQUENCE_NO = :sql_sequence_no ;

spool cctrk_10.txt
select plan_table_output
from   table(dbms_xplan.display('PLAN_TABLE',
          'cctrk_10', 'ALL'));
spool off

prompt end of explain plan for statment #10

I want to write a shell script to change the names of the files to a combination of the program and function. For example, cctrk_10.sql will be cctrk_dmp_inbc_s.ppc_dmp_dl_inbc_by_pk, and every occurance of #number, cctrk_number, cctrk_number.txt should also be a combination of the program and function. For example, #10 will be #dmp_inbc_s.ppc_dmp_dl_inbc_by_pk,
cctrk_10 will be cctrk_dmp_inbc_s.ppc_dmp_dl_inbc_by_pk, and cctrk_10.txt will also be cctrk_dmp_inbc_s.ppc_dmp_dl_inbc_by_pk.txt.

Any help will be greatly appreciated.

Question by:jkopoku
    LVL 84

    Expert Comment

    perl -i -pe '$x{$1}=$2 if /-- (Program|Function):\s*(\S+)/; if( eof ){ $r="cctrk_$x{Program}_$x{Function}"; rename $ARGV,$r or warn "$ARGV $r $!"}' cctrk_*.sql

    Author Comment

    Thanks for helping me. Your script changes the files name alright, but the content does not change.
    LVL 84

    Expert Comment

    What do you want to change in the content?

    Author Comment


    cctrk_10.txt to cctrk_program_function.txt
    cctrk_10 to cctrk_program_function
     #10 to # program_function

    This number is based on the name of the file, therefore, it will be different in other files. It is 10 because the name of the file is cctrk_10.sql


    Author Comment

    Maybe I am not very clear. I want all change all occurance of cctrk_10.txt to cctrk_the new file name.txt. So in this case cctrk_10.txt will change to cctrk_dmp_inbc_s.ppc_dmp_dl_inbc_by_pk.txt,  cctrk_10 will change to  cctrk_dmp_inbc_s.ppc_dmp_dl_inbc_by_pk, and #10 will become #dmp_inbc_s.ppc_dmp_dl_inbc_by_pk


    LVL 84

    Accepted Solution

    perl -i -pe '$x{$1}=$2 if /-- (Program|Function):\s*(\S+)/; $x="$x{Program}_$x{Function}"; ($n)=$ARGV=~/(\d+)/; s/(#|cctrk_)$n/$1$x/g; if( eof ){ $r="cctrk_$x"; rename $ARGV,$r or warn "$ARGV $r $!"}' cctrk_*.sql

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Suggested Solutions

    Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
    FreeBSD on EC2 FreeBSD ( is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
    This tutorial goes over how to archive and restore FreeBSD jails that are managed by ezjail.
    Video by: Phil
    This video goes over how to configure and start a jail in FreeBSD.  This video is meant to supplement the article included with this course.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now