Solved

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

Posted on 2004-04-20
6
264 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
move a line in eclipse 3 83
sum13 challenge 24 90
sumHeights2  challenge 7 89
Can not remove SSL certificate on iPhone 6 - iOS10.2 12 286
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now