Appending a line in a file after a particular line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Appending a line in a file after a particular line
# 1  
Old 12-12-2007
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  
Old 12-12-2007
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  
Old 12-12-2007
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  
Old 12-12-2007
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  
Old 12-12-2007
Quote:
Originally Posted by maxvirrozeito
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  
Old 12-12-2007
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  
Old 12-12-2007
The solution with sed worked fine as well.

Thanks !!
Max
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash - Appending to specific line in file

I'm working on a personal project, a multiplication quiz script for my kids. In it, the user's performance will be recorded and written to a file. After they've played it a little while, it will start to focus more on the ones that give them the most trouble-- that take a long time to answer or... (4 Replies)
Discussion started by: treesloth
4 Replies

2. UNIX for Dummies Questions & Answers

Appending Date at the end ONLY in first line of file

Hi, My requirement is to append a date in format DDMMYYYYHHMISS at the end of first line of file which is HEADER. I am trying command sed -i '1s/.*/&<date_format>/' <file_name> Where <date_format>=`date +%m%d%Y%H%M%S` I am somehow misisng the right quotes ti get this added in above... (2 Replies)
Discussion started by: sanjaydubey2006
2 Replies

3. Shell Programming and Scripting

appending data to last line of file

A friend contacted me recently with an interesting question. We got something worked out, but I'm curious what answers you all can come up with. Given a shell script (in bash) that processes a bunch of data and appends it to a file, how would you append the date, time, and a filename to the... (6 Replies)
Discussion started by: malcolmpdx
6 Replies

4. Shell Programming and Scripting

Appending the first word of each line to the end of each line

Hi Experts, Am relatively new to shell programming so would appreciate some help in this regard. I am looking at reading from a file, line by line, picking the first word of each line and appending it to the end of the line. Any suggestions? INPUT FILE - 3735051 :... (7 Replies)
Discussion started by: hj007
7 Replies

5. Shell Programming and Scripting

awk;sed appending line to previous line....

I know this has been asked before but I just can't parse the syntax as explained. I have a set of files that has user information spread out over two lines that I wish to merge into one: User1NameLast User1NameFirst User1Address E-Mail:User1email User2NameLast User2NameFirst User2Address... (11 Replies)
Discussion started by: walkerwheeler
11 Replies

6. Shell Programming and Scripting

sed: appending alternate line after previous line

Hi all, I have to append every alternate line after its previous line. For example if my file has following contents line 1: unix is an OS line 2: it is open source line 3: it supports shell programming line 4: we can write shell scripts Required output should be line1: unix is an OS it is... (4 Replies)
Discussion started by: rish_max
4 Replies

7. Shell Programming and Scripting

Appending a column in one file to the corresponding line in a second

It appears that this has been asked and answered in similar fashions previously, but I am still unsure how to approach this. I have two files containing user information: fileA ttim:/home/ttim:Tiny Tim:632 ppinto:/home/ppinto:Pam Pinto:633 fileB ttim:xkfgjkd*&#^jhdfh... (3 Replies)
Discussion started by: suzannef
3 Replies

8. Shell Programming and Scripting

Appending line ending with '}" to new line

Hello masters. I have a rather simple problem but its been killing me. I have a file "x" with only 1 line inside it. The line looks something like Now this is only part of the line. Its actually about 4000 characters. What i need to do is whenever there is a "}", i need to append the next... (4 Replies)
Discussion started by: aismann
4 Replies

9. Shell Programming and Scripting

Appending the line number and a seperator to each line of a file ?

Hi, I am a newb as far as shell scripting and SED goes so bear with me on this one. I want to basically append to each line in a file a delimiter character and the line's line number e.g Change the file from :- aaaaaa bbbbbb cccccc to:- aaaaaa;1 bbbbbb;2 cccccc;3 I have worked... (4 Replies)
Discussion started by: pjcwhite
4 Replies

10. Shell Programming and Scripting

Appending data at the first and last line of a file

Hi, Am trying to write a shell script which will append a header and a footer to an existing file. Header will contain details like the current date while the footer will contain the no: of records listed in the file. I know we can use the CAT command, but i have no clue abt the syntax to... (4 Replies)
Discussion started by: brainstormer
4 Replies
Login or Register to Ask a Question