LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 04-26-2016, 12:52 AM   #1
gglq000
Member
 
Registered: Mar 2012
Posts: 47

Rep: Reputation: Disabled
need to find a solution: replace every known string in all files in certain directory


This is a common dilemma I think encountered with anyone who had worked with large software base. Single variable name or some change of implementation, and need to replace every occurrence of that name in every file in throughout the code base.

I am tasked with major overhaul and wondering is there any code/software or easy to use shell script can be made so that this can be done.

For example, I need to replace every <string1> with <string2> in every file, with extension .py, RECURSIVELY in certain directory.
 
Old 04-26-2016, 05:42 AM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,230

Rep: Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724
Code:
find /certain/directory -name "*.py" | xargs sed -i 's/string1/string2/g'
 
2 members found this post helpful.
Old 04-26-2016, 06:30 AM   #3
Michael Uplawski
Member
 
Registered: Dec 2015
Location: Outside, most of the time.
Posts: 866
Blog Entries: 25

Rep: Reputation: 527Reputation: 527Reputation: 527Reputation: 527Reputation: 527Reputation: 527
Quote:
Originally Posted by gglq000 View Post
This is a common dilemma I think encountered with anyone who had worked with large software base. Single variable name or some change of implementation, and need to replace every occurrence of that name in every file in throughout the code base.
The solution by keefaz is generally applicable.

However, if we talk about symbols in program code, many IDEs have refactoring-capabilities. As you mention “the common dilemma”, I had often done this kind of refactoring in Java-code with the help of some Eclipse- or RSA-functionality and in C++/Qt-code with Qt-Creator. Even if I ignore the tools that are already invented for Python, there should be an established procedure for this language, too. Or you should take keefaz' command-example and store it as a macro, somewhere...

Each solution has its pitfalls.

Last edited by Michael Uplawski; 04-26-2016 at 06:31 AM.
 
Old 04-28-2016, 08:54 PM   #4
gglq000
Member
 
Registered: Mar 2012
Posts: 47

Original Poster
Rep: Reputation: Disabled
thanks, my dev environment is very simple not using IDE, just using nano as text editor. So I will give a try on keefaze script.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Search and replace specific string in different files in a directory using shell. Lucifer999 Programming 8 06-26-2011 07:37 AM
[SOLVED] How to find a string in files under the current directory? thomas2004ch Linux - Newbie 4 10-05-2010 07:41 AM
Find & Replace string in multiple files Rudy Linux - General 14 04-15-2010 09:10 AM
Find and replace string Johng Programming 9 01-13-2010 05:50 AM
Find string pattern in directory of text files magnum818 Linux - Newbie 2 10-15-2003 09:19 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 05:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration