LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   replace text in files and directories (https://www.linuxquestions.org/questions/linux-software-2/replace-text-in-files-and-directories-245566/)

rincewind 10-21-2004 11:22 AM

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...

jschiwal 10-24-2004 03:10 AM

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.

fiomba 10-24-2004 06:36 PM

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...

jschiwal 10-26-2004 10:47 PM

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.

fiomba 10-27-2004 11:29 AM

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...


All times are GMT -5. The time now is 02:45 AM.