Solved

Appending a file to all specified files (*.cpp) recursively

Posted on 2004-04-20
6
267 Views
Last Modified: 2013-12-26
Hi,

I'd like to append some copy consturctor code

Foo::Foo(const Foo&r) // copy constructor
{
  copy ( r);
}
Foo& Foo::operator=(const Foo&r) // assgnment operator
{
   return copy ( r);
}
Foo& Foo::copy(const Foo&r)
{

    // assign each element to is corresponding value in our object

   return *this;
}

to the end of all my *.cpp files recursively.

I know manually, I can do

cat constructorfile.txt >> acppfile.cpp

but I'd like this to be automated.

Also, if you can, if the file that I just appended to can replace the instance of "Foo" with the filename (whihc is the same as the class... like MyClass.cpp), that would be most helpful.

Thanks!

-Edward

0
Comment
Question by:edwardt
6 Comments
 
LVL 3

Expert Comment

by:dtkerns
ID: 10872259
hmm looks like almost the same Q you just asked ...

if you have a directory tree use find

find . -type f -name \*.cpp | while read f
do
      awk 'BEGIN { f ='$f'; sub(".cpp$", "", f) }; { x = $0; gsub("Foo", f, x); print x }' constructorfile.txt >> $f
done

if they are all in 1 dir you could simplify the first line to:

for f in *.cpp
0
 
LVL 9

Expert Comment

by:HamdyHassan
ID: 10872309

for i in *.cpp
do
   cat constructorfile.txt >> $i
done
0
 

Author Comment

by:edwardt
ID: 10872796
Hi,

I get the following error from the above code.

awk: cmd. line:1: BEGIN { f =./Args.cpp; sub(".cpp$", "", f) }; { x = $0; gsub("Foo", f, x); print x }
awk: cmd. line:1:            ^ parse error

Thanks.

-Edward
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:edwardt
ID: 10872841
Also, in my files, Foo is replace by ./filename, like


./Args::./Args(const ./Args&r) // copy constructor
{
  copy ( r);
}
./Args& ./Args::operator=(const ./Args&r) // assgnment operator
{
   return copy ( r);
}
./Args& ./Args::copy(const ./Args&r)
{
0
 
LVL 84

Expert Comment

by:ozo
ID: 10872932
find . -name \*.cpp -exec sh -c 'cat constructorfile.txt >> {}' \;
0
 
LVL 3

Accepted Solution

by:
dtkerns earned 500 total points
ID: 10879644
sorry, forgot find would add a ./

 try this


find . -type f -name \*.cpp | while read f
do
      awk 'BEGIN { f = "'$f'"; sub("^./", "", f); sub(".cpp$", "", f) }; { x = $0; gsub("Foo", f, x); print x }' constructorfile.txt >> $f
done
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question