Find text containing paths and replace with a string in all the python files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find text containing paths and replace with a string in all the python files
# 1  
Old 09-20-2010
Bug Find text containing paths and replace with a string in all the python files

I have 100+ python files in a single directory. I need to replace a specific path occurrence with a variable name.

Following are the find and the replace strings:

Findstring--"projects\\Debugger\\debugger_dp8051_01\\debugger_dp8051_01.cywrk"

Replacestring--self.projpath

I tried following without success

for y in *;
do sed "s/\"projects\\Debugger\\debugger_dp8051_01\\debugger_dp8051_01.cywrk\"/self.projpath/g" $y > temp;
mv temp $y;
done

Can someone please help me with the correct solution?
# 2  
Old 09-20-2010
Post your sample input file.
# 3  
Old 09-20-2010
Code:
for y in *;
do perl -pe 's/\Q"projects\\Debugger\\debugger_dp8051_01\\debugger_dp8051_01.cywrk"\E/self.projpath/' $y > temp;
mv temp $y;
done

# 4  
Old 09-20-2010
Code:
$ ruby  -i.bak  -ne 'BEGIN{o=%q(projects\\\\Debugger\\\\debugger_dp8051_01\\\\debugger_dp8051_01.cywrk)};print if gsub(o,"self.projpath")' *.py

# 5  
Old 09-20-2010
Code:
awk '{gsub(/"projects\\\\Debugger\\\\debugger_dp8051_01\\\\debugger_dp8051_01.cywrk"/,"self.projpath")}1' *.py
sed 's/"projects\\\\Debugger\\\\debugger_dp8051_01\\\\debugger_dp8051_01.cywrk"/self.projpath/' *.py

# 6  
Old 09-20-2010
It worked correctly for me now...

Thanks for your quick responses.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Find all .sh files in file system and need to replace the string inside .sh files

Hi All, I need to write a script to find all "*.sh" files in /home file system and if any string find "*.sh" files with the name vijay@gmail.com need to replace with vijay.bhaskar@gmail.com. I just understood about the find the command to search .sh files. Please help me on this. find / -name... (3 Replies)
Discussion started by: bhas85
3 Replies

2. UNIX for Beginners Questions & Answers

Find and replace a string in a text file

Dear all, I want to find all the "," in my text file and then replace the commas to a tab. I found a script online but I don't know how to modify the script for my case. Any one can help? Thank you. @echo off &setlocal set "search=%1" set "replace=%2" set "textfile=Input.txt" set... (2 Replies)
Discussion started by: forevertl
2 Replies

3. Programming

Python: Check 2 text files for string and print contexts

I have 2 text files: cities.txt San Francisco Los Angeles Seattle Dallas master.txt Atlanta is chill and laid-back. I love Los Angeles. Coming to Dallas was the right choice. New York is so busy! San Francisco is fun. Moving to Boston soon! Go to Seattle in the summer. ... (0 Replies)
Discussion started by: pxalpine
0 Replies

4. Shell Programming and Scripting

Replace directory paths in multiple files at once

I need to update about 2400 files in a directory subtree, with a new directory path inside the files I need to change this occurence in all files: /d2/R12AB/VIS/apps/tech_st/10.1.2 with this: /u01/PROD/apps/apps_st/10.1.3 I know how to change single words using "find . -type f -print0 |... (6 Replies)
Discussion started by: wicus
6 Replies

5. Shell Programming and Scripting

Find and replace using 2 text files as arrays.

Here's the nonfunctional code I have so far #!/bin/bash searchFor=(`cat filea.txt` ) replaceWith=(`cat fileb.txt`) myMax=${#searchFor} myCounter=1 while ; do sed -i 's/${$searchFor}/${$replaceWith}/g' done The goal is to use each line in filea.txt as a search term, and each line... (2 Replies)
Discussion started by: Erulisseuiin
2 Replies

6. Shell Programming and Scripting

Find and add/replace text in text files

Hi. I would like to have experts help on below action. I have text files in which page nubmers exists in form like PAGE : 1 PAGE : 2 PAGE : 3 and so on there is other text too. I would like to know is it possible to check the last occurance of Page... (6 Replies)
Discussion started by: lodhi1978
6 Replies

7. Shell Programming and Scripting

find and replace a search string recursively in files

Hi , I have a directory structure as dir and subdirectories and files under it and so on.now I need to find the files which contain the search string under every dir and subdir and replace . my search string is like searchstring=/a/b string to be replaced=/a/c/b please help. ... (7 Replies)
Discussion started by: mohanpadamata
7 Replies

8. Shell Programming and Scripting

To find and replace paths consisting \

Hi all, I am new to unix and wanted to replace the string consisting \ with nothing. I tried the following but did not help. for y in `ls DIV*`; do sed s/C:\\Project\\AML\\bin//g $y > temp mv temp $y done I actually want to remove any occurance of C:\Project\ABC\bin in... (4 Replies)
Discussion started by: cv_pan
4 Replies

9. Shell Programming and Scripting

find and replace string in a directory files

Hi, I have a directory has DIR1 and the D1 directory has 200+ files. I want change the string from "Bangalore" to "Bangaluru" in all files in the D1 directory. Thanks (2 Replies)
Discussion started by: koti_rama
2 Replies

10. UNIX for Dummies Questions & Answers

Find and replace a string in multiple files

I used the following script cd pathname for y in `ls *`; do sed "s/ABCD/DCBA/g" $y > temp; mv temp $y; done and it worked fine for finding and replacing strings with names etc. in all files of the given path. I'm trying to replace a string which consists of path (location of file) ... (2 Replies)
Discussion started by: pharos467
2 Replies
Login or Register to Ask a Question