Solved

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

Posted on 2004-04-20
6
263 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
format the code in java 6 72
sum67 challenge 35 89
Unix Command -- Challenging  question 7 79
Annoying "thing" blocks my view 4 50
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

707 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

15 Experts available now in Live!

Get 1:1 Help Now