The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Appending line number to each line and getting total number of lines chiru_h Shell Programming and Scripting 2 03-25-2008 10:19 AM
capturing line from script output and appending to a file wally_welder Shell Programming and Scripting 6 08-31-2007 03:03 AM
Appending line ending with '}" to new line aismann Shell Programming and Scripting 4 08-13-2007 02:09 AM
Appending the line number and a seperator to each line of a file ? pjcwhite Shell Programming and Scripting 4 03-21-2007 01:29 AM
Appending data at the first and last line of a file brainstormer Shell Programming and Scripting 4 01-03-2007 10:38 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-12-2007
maxvirrozeito maxvirrozeito is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 10
Appending a line in a file after a particular line

Hello,

I have got a C file in which I would like to add an include statement of my own.
There are already a few include statements and mine should come right after the last existing one (to be neat).

With grep I can get the lines containing the word 'include' and I guess I should feed the last line of the result to a sed command to append a line but I don't know exactly how to do this part. Any suggestions ?

Thanks a lot in advance
Max
  #2 (permalink)  
Old 12-12-2007
porter porter is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2007
Posts: 2,965
The normal approach to editing C files is to use a text editor such as vi or emacs.

Are you trying to update many C source files?
  #3 (permalink)  
Old 12-12-2007
maxvirrozeito maxvirrozeito is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 10
Yes, this is part of the process of updating many C files which cannot be reasonably made by hand.

by using the following:

Code:
sed '/#include/ a\
#include "myHeader.h"' input > ouput
it adds my header after each line already calling a header. Is there a means to make this happen only after the last existing include ?

Max
  #4 (permalink)  
Old 12-12-2007
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
If awk is allowed:

Code:
awk ' 
$1=="#include"{f=1}
$1!="#include" && f{f=0;print str}1
' str="#include My.h" file.c
Regards
  #5 (permalink)  
Old 12-12-2007
shamrock shamrock is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2007
Location: USA
Posts: 750
Quote:
Originally Posted by maxvirrozeito View Post
Yes, this is part of the process of updating many C files which cannot be reasonably made by hand.

by using the following:

Code:
sed '/#include/ a\
#include "myHeader.h"' input > ouput
it adds my header after each line already calling a header. Is there a means to make this happen only after the last existing include ?

Max
Code:
sed '/^#include/{
n;/^#include/!{i\
#include "myHeader.h"
}
}' input > output
  #6 (permalink)  
Old 12-12-2007
maxvirrozeito maxvirrozeito is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 10
Brilliant, that's working like magic !

However, I don't understand at all the syntax of your code so I'll have a look at awk in further details.

Thanks
Max
  #7 (permalink)  
Old 12-12-2007
maxvirrozeito maxvirrozeito is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 10
The solution with sed worked fine as well.

Thanks !!
Max
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:20 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0