Tuesday, 3 November 2009

find and perl

I want to find a set of files table_pid_id1.xml , table_pid_id2.xml and table_pid_id3.xml with ids witten inside the xmls in the first line as " tableid='id1' rowid='pid' " etc. Also, i want to rename these files to table_newpid_id1.xml etc and replace pid inside each file with "newpid" .This is the way i can do using combination of find and perl.

Assume dir structure as dir1/dir2/table_pid_id1.xml etc..

Command to find these files and replace the strings inside xml file using perl :

a)first find the files and rename them from table_pid_id1.xml to table_newpid_id1.xml etc..

for fn in `find dir1/ -maxdepth 2 -name "*pid*" ` ; do echo $fn ; mv $fn $(echo $fn | sed 's/pid/newpid/') ;done

b)then, find the new files and replace the pid inside the files with newpid .

find dir1/ -maxdepth 2 -name "*pid_*.xml" | xargs -i perl -i.bak -wple '$_ =~ s/'pid'/'newpid'/g;' {} ;

No comments:

Post a Comment

Tweets by @sriramperumalla