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

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 ...

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

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.

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

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

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

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

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

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

It prints the whole file and adds a new line 980 if it gets that far.

Hi Era,

I think 980 line will be as it is ,

The content we entered will be 981 line

Yes, sorry for being inexact. The point I wanted to make was actually that if the file has less than 980 lines, it will do nothing.

Yes I know the difference between 2 files, but I don't know how sed works :slight_smile: ..

is it possible that it writes to a line number 981 but the file has only 980 lines up to now.. without printing the whole file if that can be avoid it would be awesome and it doesn't have to be sed .. whatever does the trick is good for me awk, sed, cat, head whatever :stuck_out_tongue:

Thank you

find here below a perl script to do what you want

print "\n Enter the line number you want to enter the data to\n";
$line = <STDIN>;
chop($line);
print "\nEnter the text to be inserted\n";
$word = <STDIN>;
chop($word);
open (OUT,">outputfile");
open (IFILE,"inputfile");
$count = 2;
while (<IFILE>) {
chop($);
print OUT ("$
\n");
if($count == $line){
print OUT ("$word\n");
}
$count++;
}
close INFILE;
close OUT;

find here below a perl script to do what you want

print "\n Enter the line number you want to enter the data to\n";
$line = <STDIN>;
chop($line);
print "\nEnter the text to be inserted\n";
$word = <STDIN>;
chop($word);
open (OUT,">outputfile");
open (IFILE,"inputfile");
$count = 2;
while (<IFILE>) {
chop($);
print OUT ("$
\n");
if($count == $line){
print OUT ("$word\n");
}
$count++;
}
close INFILE;
close OUT;