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... |
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. |
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] Code:
for cpp_files in /dir_containing_cpp_files/*.cpp My solution is: Code:
n=0 #counter and list initialization 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 Code:
for i in $LIST_CUMUL Code:
echo $LIST_CUMUL |
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. |
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. |