insert multiple lines into a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting insert multiple lines into a file
# 1  
Old 05-18-2008
insert multiple lines into a file

Hi all,

I've got some problems with editing a big configuration file .. its about 2k lines long.. anyway what I need is to place certain text to certain line number.. lets say I need to place "Something" on line 980 .. "something" else on line number 1500 and so on without tempering the rest of the configuration file ... I found somewhere this
Code:
head -981 conf.temp > conf.conf; echo "Email myemail@email.com" >> conf.conf; sed '4,$!d' <conf.temp >> conf.conf

it seems to work on one occasion but when I want to make another change in the configuration whole thing comes messed up, things appear double and so on ... any help ?

Thank you in advance ...
# 2  
Old 05-18-2008
HI,

U can used sed for this.

If u want to do some change on line 980,

sed '980s/anything/anything/' filename.

or if u want to insert a line after 980, u can use append (a) option of sed.

Thanks
Penchal
# 3  
Old 05-18-2008
Your question basically boils down to "when is it safe to change something into something else"? Without knowledge of the file format you are manipulating, we can't really answer that question. But in broad terms, what you usually end up doing is examine more of the context. For example, instead of blindly modifying line number four, look for a line containing xxxx followed by a line containing yyyy, and only insert a line between those two if they are found. Whether to fail silently or throw an error if the change cannot be made obviously depends a lot on the context and the application.

Another common trick is to insert a comment block or something, and replace the comment block automatically.

Code:
## WARNING: vvvv automatically generated configuration, do not edit
setting='whatever your script puts here'
## WARNING: ^^^^

# 4  
Old 05-18-2008
Era : I've closely examinded the configuration file and there are some 10 or 11 lines to be changed or modified.. and I know exactly which ones .. Although your approach is good and maybe better as well, just because I don't know how to do it I find line manipulation easier..thank you for your answer

penchal_boddu
how exactly should I use a option of sed ... is it like this
Code:
sed -a '980s/anything/anything/' filename.

if yes will this create a line if it reaches the end of the file or if the line is existing will it modify it ? Thank you
# 5  
Old 05-18-2008
for example if u want to append to 980 line,

sed '980a\
Content_u_want_to_Enter' file

This will insert Content_u_want_to_Enter in 981 line.
Thanks
Penchal
# 6  
Old 05-18-2008
penchal_boddu,
I've tried it for several times and it doesn't work .. what it does it prints the whole file like I was using cat file .. any other solutions ? thank you
# 7  
Old 05-18-2008
use redirection operatot to get the data into a file.

sed '980a\
Content_u_want_to_Enter' file >> new_file

Make a diff between these two files. U can know the difference between the two files and also how sed works.


Thanks
Penchal
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing multiple lines from input file, if multiple lines match a pattern.

GM, I have an issue at work, which requires a simple solution. But, after multiple attempts, I have not been able to hit on the code needed. I am assuming that sed, awk or even perl could do what I need. I have an application that adds extra blank page feeds, for multiple reports, when... (7 Replies)
Discussion started by: jxfish2
7 Replies

2. Shell Programming and Scripting

Want to Insert few lines which are stored in some file before a pattern in another file

Hello, I have few lines to be inserted in file_lines_to_insert. In another file final_file, I have to add lines from above file file_lines_to_insert before a particular pattern. e.g. $ cat file_lines_to_insert => contents are abc def lkj In another file final_file, before a... (6 Replies)
Discussion started by: nehashine
6 Replies

3. Shell Programming and Scripting

Insert few lines above a match using sed, and within a perl file.

Greetings all, I am trying to match a string, and after that insert a few lines above that match. The string is "Version 1.0.0". I need to insert a few lines ONLY above the first match (there are many Version numbers in the file). The rest of the matches must be ignored. The lines I need to... (2 Replies)
Discussion started by: nagaraj s
2 Replies

4. Shell Programming and Scripting

CSV to SQL insert: Awk for strings with multiple lines in csv

Hi Fellows, I have been struggling to fix an issue in csv records to compose sql statements and have been really losing sleep over it. Here is the problem: I have csv files in the following pipe-delimited format: Column1|Column2|Column3|Column4|NEWLINE Address Type|some descriptive... (4 Replies)
Discussion started by: khayal
4 Replies

5. Shell Programming and Scripting

how to Insert values in multiple lines(records) within a pipe delimited text file in specific cols

this is Korn shell unix. The scenario is I have a pipe delimited text file which needs to be customized. say for example,I have a pipe delimited text file with 15 columns(| delimited) and 200 rows. currently the 11th and 12th column has null values for all the records(there are other null columns... (4 Replies)
Discussion started by: vasan2815
4 Replies

6. Shell Programming and Scripting

split row into lines and insert file name

I have a directory with several hundred files. The file format is a space delimited row with an unknown number of columns: A B C D E F G ... I need to turn this format File1 A File1 B File2 A File3 A File3 B File3 C ... I can use grep to display the filename next to each row of... (2 Replies)
Discussion started by: newreverie
2 Replies

7. Shell Programming and Scripting

sed/awk to insert multiple lines before pattern

I'm attempting to insert multiple lines before a line matching a given search pattern. These lines are generated in a separate function and can either be piped in as stdout or read from a temporary file. I've been able to insert the lines from a file after the pattern using: sed -i '/pattern/... (2 Replies)
Discussion started by: zksailor534
2 Replies

8. Shell Programming and Scripting

Insert 2 lines in a file at a specific location

Hi, I need to insert two new lines in a file: The file: "..... ...... ULIMIT_MAX_FILES="ulimit -S -n `ulimit -H -n`" .... .... " I need to add the lines: LD_LIBRARY_PATH='$LD_LIBRARY_PATH:$APACHE_HOME/modules' DOWNLOADMODULE_CONF_PATHNAME='$APACHE_HOME/conf/DWLModule.cfg' right... (2 Replies)
Discussion started by: potro
2 Replies

9. Shell Programming and Scripting

(sed) parsing insert statement column that crosses multiple lines

I have a file with a set of insert statements some of which have a single column value that crosses multiple lines causing the statement to fail in sql*plue. Can someone help me with a sed script to replace the new lines with chr(10)? here is an example: insert into mytable(id, field1, field2)... (3 Replies)
Discussion started by: jjordan
3 Replies

10. Shell Programming and Scripting

Insert lines at specific location in file

Hi There I have this file that I would like to add entries to, however, there is a "}" as the last line that I need to keep. Basically i would like to know how I can write a script that will add new lines at the second to last line position (ie always add new line above the close bracket) ... (17 Replies)
Discussion started by: hcclnoodles
17 Replies
Login or Register to Ask a Question