Shell script to change files name

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
DELETE FROM INCOL_BATCH_CONTROL
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.




jkopokuAsked:
Who is Participating?
 
ozoCommented:
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
0
 
ozoCommented:
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
0
 
jkopokuAuthor Commented:
Thanks for helping me. Your script changes the files name alright, but the content does not change.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
ozoCommented:
What do you want to change in the content?
0
 
jkopokuAuthor Commented:

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


0
 
jkopokuAuthor Commented:
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

Thanks.



0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.