LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 10-21-2004, 11:22 AM   #1
rincewind
Member
 
Registered: Mar 2004
Posts: 32

Rep: Reputation: 15
replace text in files and directories


Hello,

Im coding a lot of c++ in my spare time.

Now I whant to replace a lot of texts in all of the files ending with .cpp in all of my directories (recursive). Yet I dont whant to replace in the directories who is hidden for me (directories like .cvs)..
Anyone have an idea how to do this in linux?
Im running debian.

Thankfull,
-- Rincewind...
 
Old 10-24-2004, 03:10 AM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Locating the .cpp files sounds like a job for the 'find' command: find ./ -name "*.cpp"

Performing the substitutions could be done by sed. You will need to make a sed script that will perform the particular substitutions that you want.

If you use the -i and -s ( in-place editing, and separate ) flags, you might not need to produce a tempory flle.


I think this should work:
find ./ -name "*.cpp" -exec sed -isf sedscript {} \;

I think that the -i and -s flags are gnu extensions. Check your manpage or sed --help output to see if you have them. I resently upgraded and found that the 'man' command no longer had the '-T' option. I would prefer to read the output of man -Tdvi <topic> in kdvi.

Last edited by jschiwal; 10-24-2004 at 03:13 AM.
 
Old 10-24-2004, 06:36 PM   #3
fiomba
Member
 
Registered: Sep 2004
Posts: 63

Rep: Reputation: 15
Hi rincewind,

I am a Linux newbie, but for that more attentive to the needs of a "collegue" (as far as Linux is concerned... you are a C++ programmer, and only for that an "expert")

I thought to know well shell programming and for that I wanted to try something more like a programming language (Python). I've read a few books in C++ programming, but I don't like it and its OOP...

Back to your problem... It has interested me mainly for the fact of the recursion on subdirs (hidden or not).

When I red the first time your post you had till that time received no answer. Now a Senior Member (jschiwal) has given his expert's opinion (to say the truth I've understood almost nothing - I hope not to have a one more enemy...).
Perhaps you has already solved on the ground of jschiwal's indications your problem...

For what it can useful... (at least for me, it has been very interesting).

The problem is the search also on the subdirs.

I use a for cycle which in Bash programming is different from C/C++ "for";
the sintax is:
Code:
for arg in[list]
do
    command(s)...
done
If your cpp files were in a single dir, the syntax should be:
Code:
for cpp_files  in /dir_containing_cpp_files/*.cpp
do
    command(s)... # text replacing instructions
done
You need to build a <list> for taking into account also subdirs.

My solution is:
Code:
n=0		               #counter  and list initialization
LIST_CUMUL=""

# pay attention to the back-quotes!
# if for ex. your dir is CPP (with subdirs sources, .include etc...)
# the for would be:
# for cpp_files in `find /CPP/*.cpp`

for cpp_files in `find /dir_containing_cpp_files/*.cpp`
do
    LIST_CUMUL="$LIST_CUMUL$cpp_files "
done
Pay attention to the blank in the generated list! The elements of the list use blanks or LFs as separators.
For this reason in your filenames blank is forbidden...
It must be substituted with "_" by means of very simple shell commands:
Code:
find /dir_containing_cpp_files/*.cpp | grep " " # search for blanks
mv "filename with blanks" filename_without_blanks
With the new list ($LIST_CUMUL) you can then run another for cycle:
Code:
for i  in $LIST_CUMUL
    command(s)... # text replacing instructions
done
If you print $LIST_CUMUL by means of:
Code:
echo $LIST_CUMUL
you will see that all your cpp files are there...

Last edited by fiomba; 10-24-2004 at 06:43 PM.
 
Old 10-26-2004, 10:47 PM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Hello fiomba,

The find command will recurse down directories by default.
The find command is in my opinion the most useful core utility. You can locate files by type, permissions, size, create date, even last access time.

The sed command stands for stream editor. Changing patterns in a large groups of files is what sed excells at.

Last edited by jschiwal; 10-26-2004 at 10:59 PM.
 
Old 10-27-2004, 11:29 AM   #5
fiomba
Member
 
Registered: Sep 2004
Posts: 63

Rep: Reputation: 15
Every time I confront myself against an expert, I receive healthy lessons of humility...

Your solution is by far the more elegant, but the problem is that requires some work to understand it.
Knowing another solution, the newbie tends to follow the few he knows...

I didn't know the find... -exec possibility. Every time my emancipation is further...
 
  


Reply



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
How to replace a string in a text file jpan Linux - General 3 10-14-2012 06:17 PM
Replace text of unknown content with other text in file brian0918 Programming 15 07-14-2005 09:22 PM
Replace text of unknown content with other text in file brian0918 Linux - Software 1 07-14-2005 03:22 PM
How to replace text in multiple files bpk Linux - Newbie 2 02-10-2004 02:03 PM
Script file to replace large text blocks in files? stodge Linux - Software 0 09-27-2003 10:53 AM

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

All times are GMT -5. The time now is 11:48 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
Open Source Consulting | Domain Registration