Search Placementyogi.com
 

How to replace a string pattern in many files using python

To Execute:

python my_replace.py pattern1 pattern2

Here is the complete file:

import os
import sys
cmd=”grep -r ‘”+sys.argv[1]+”' * | cut -d ':' -f 1,1 | uniq >  file_list.txt ”
os.system(cmd)
fp=open('file_list.txt','read')
for line in fp.readlines():
        print line 
        cmd=”sed -i 's/”+sys.argv[1]+”/”+sys.argv[2]+”/g' "+line
        os.system(cmd)

Explanation:

Find the pattern in multiple files and store the filenames in a file_list.txt file

grep -r ‘pattern1' * | cut -d ':' -f 1,1 | uniq >file_list.txt

 

Let’s Break the above command  into segments:

The 1st segment is:

grep –r ‘pattern1’ *

Here  we are searching for the pattern in all the files under current directory recursively.

 

The output of this command is :

dir1/file_under_dir1:pattern1 pattern1 pattern1

dir1/file_under_dir1:pattern1 pattern1

dir1/dir_under_dir1/file_inside_dir_under_dir1:pattern1 pattern1 pattern1

dir1/dir_under_dir1/file_inside_dir_under_dir1:pattern1 pattern1

dir2/file_under_dir2:pattern1 pattern1 pattern1

dir2/file_under_dir2:pattern1 pattern1

file1:pattern1 pattern1 pattern1

file1:pattern1 pattern1

Note:

1. The first column is the filename where the pattern1 is present.

2. Each line is printed where the pattern1 was present.

The 2nd segment is :

cut -d ':' -f 1,1

Here we are cutting only the first field of the output of 1st segment which contains the filename.

Command: grep -r 'pattern1' * | cut -d ':' -f 1,1

Output:

dir1/file_under_dir1

dir1/dir_under_dir1/file_inside_dir_under_dir1

dir2/file_under_dir2

file1

So, Now we have the file names list that contains the pattern.

All we need to do now is to store these names into a file which can be done through indirection operator (>).

And the complete command becomes:

grep -r ‘pattern1' * | cut -d ':' -f 1,1 | uniq >  file_list.txt

Now , Here is the python file that reads each line of the file_list.txt and replaces the pattern1 with pattern2.

Save this python file as my_replace.py

import os

fp=open('file_list.txt','read')

for line in fp.readlines():

        print line

        os.system("sed -i 's/pattern1/pattern2/g' "+line)

Execute: python my_replace.py

We can use command line arguments also to get the pattern1 and pattern2 :

We can also execute the above file_list creation command inside the python file.

python my_replace.py pattern1 pattern2

Here is the complete file :

import os

import sys

cmd=”grep -r ‘”+sys.argv[1]+”' * | cut -d ':' -f 1,1 | uniq >  file_list.txt ”

os.system()

fp=open('file_list.txt','read')

for line in fp.readlines():

        print line

        cmd=”sed -i 's/”+sys.argv[1]+”/”+sys.argv[2]+”/g' "+line

        os.system(cmd)

 

Done!!!


Ravi 6/20/2013 6:10:49 PM
Thanks!! Could you please explain the sed command also.
Admin 3/9/2014 10:55:08 AM

Testing

Preview

Thanks for posting your comment! It will be visible soon!
Post Another Comment

1
+
2
=

Name:
Email :
Banner to LogicGuns.com

Hide Page Information