How to include 1 line after the last #include in a bunch of c-files ?

How to include 1 line after the last #include in a bunch of c-files ?

Post by Peter Dierc » Wed, 21 Apr 2004 03:26:24



All,

I have to insert 1 line after the last #include <...> statement in a c-file.
This has to be repeated for another +/- 100 c-file.

I thought it could be done with the "sed" command but I can not find a way to
get it working.

Thanks a lot,

Peter Dierckx

 
 
 

How to include 1 line after the last #include in a bunch of c-files ?

Post by Alan Conno » Wed, 21 Apr 2004 03:53:52



Quote:

> All,

> I have to insert 1 line after the last #include <...> statement in a c-file.
> This has to be repeated for another +/- 100 c-file.

> I thought it could be done with the "sed" command but I can not find a way to
> get it working.

> Thanks a lot,

> Peter Dierckx

Is there anything about that last #include statement that sets it apart from
the others in each file?

AC

 
 
 

How to include 1 line after the last #include in a bunch of c-files ?

Post by Michael Heimin » Wed, 21 Apr 2004 04:07:27


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
NotDashEscaped: You need GnuPG to verify this message


> All,
> I have to insert 1 line after the last #include <...> statement in a c-file.
> This has to be repeated for another +/- 100 c-file.
> I thought it could be done with the "sed" command but I can not find a way to
> get it working.

Sounds like a job for awk:

awk '/^#include/{m=1}m&&/^$/{print "whatever";m=0}{print}' infile

Or wait the few minutes, until some sed guru comes along...;)

--
Michael Heiming (GPG-Key ID: 0xEDD27B94)

Remove +SIGNS and www. if you expect an answer, sorry for
inconvenience, but I get tons of spam.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQFAhCNuAkPEju3Se5QRAiknAKDHDvJFZR83UzMiYqa2SWWM4RWStQCfbsSg
swxmbefXwJmg+BZdSFk2/ms=
=reB4
-----END PGP SIGNATURE-----

 
 
 

How to include 1 line after the last #include in a bunch of c-files ?

Post by Charles Dem » Wed, 21 Apr 2004 04:15:44




>All,

>I have to insert 1 line after the last #include <...> statement in a c-file.
>This has to be repeated for another +/- 100 c-file.

>I thought it could be done with the "sed" command but I can not find a way to
>get it working.

Assuming that each of your include statements is all on one line,
and tac is available:

tac infile |
awk '/[#]include / && c==0 {print "your stuff";c==1}{print}' |
tac > outfile

or

awk '{a[NR]=$0}
     /[#]include / {c=NR}
     END {for(i==1;i<=NR;i++)
         {print a[i];
          if(i==NR) {print "your stuff"}}}' infile > outfile

both are untested.

Chuck Demas

--
  Eat Healthy        |   _ _   | Nothing would be done at all,

  Die Anyway         |    v    | That no one could find fault with it.

 
 
 

How to include 1 line after the last #include in a bunch of c-files ?

Post by rakesh shar » Wed, 21 Apr 2004 14:14:50



> I have to insert 1 line after the last #include <...> statement in a c-file.
> This has to be repeated for another +/- 100 c-file.

> I thought it could be done with the "sed" command but I can not find a way to
> get it working.

sed -e '
   /#include </!{
      x
      //i\
NEWTEXT1\
NEWTEXT2\
NEWTEXT_LAST
      x
   }
   h
' yourfile

LN="`sed -ne '/#include </=' yourfile | sed -e '$!d'`"
sed -e "
   ${LN}a\\
NEWTEXT1\\
NEWTEXT2\\
NEWTEXT_LAST
" yourfile

 
 
 

How to include 1 line after the last #include in a bunch of c-files ?

Post by John W. Krah » Wed, 21 Apr 2004 16:39:09



> I have to insert 1 line after the last #include <...> statement in a c-file.
> This has to be repeated for another +/- 100 c-file.

perl -0pe's/^(#include\s+<.+\n)(?!(?s:.*?)^#include\s+<)/$1new text\n/m' *.c

John
--
use Perl;
program
fulfillment

 
 
 

1. SSI includes including relative includes?

Using apache's server-side includes, is there a way to have a document
include a 'master' template which in turn includes some other files
that are located relative to the original document, not the other
inclusion?  The idea here is to be able to add the same header to
a large number of documents but make them appear with navigation
aids that relate to their file system location.  It looks like
includes are always processed relative to the currently active location
so to make the scheme work a copy of the master template will have
to be in each subdirectory instead of having only one at an absolute
location.

Les Mikesell

2. Permission denied

3. UNIX include files = linux include files?

4. Slackware instalation question

5. include file error--/usr/include/linux

6. PCI modem setup...

7. Linux 0.97pl1 include files v. include/sys

8. Personal Web Server Hosting Promotion (US$7.50/Month)

9. get the last line of a bunch of files

10. WTB: bunch of equal x86 SMP (dual) motherboards including CPUs

11. ANSIfy include/linux/a.out.h and include/linux/nls.h

12. GCC-2.5.8 won't compile [/usr/include/limits.h:52: No include path...

13. RFC: moving ACPI includes under include/