?
Solved

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

Posted on 2004-04-20
6
Medium Priority
?
270 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 

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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Suggested Courses

770 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