replacing first line or lines in a file

hey guys, how do i replace only a line within a file without messing up the rest of the contents of the file?

see, if possible can you guys give me a straight forward way to do this. i dont want a complex command. what i mean is i know i can accomplish this by using sed, well, i think i can, but i keep circling around and dont know the exact command.

what i'm trying to do is run a script remotely that checks diskspace on a server. if diskspac is above a certain percentage this script will look at the file i'm trying to edit and delete files on the disk based on the threshold, number of days, given in the file.

this may be confusing but really all i need is the one liner that will just replace a line for me if the script fails to free up enough space. thanks guys.

Are you looking for something like this:

# cat test
this is line 1
this is line 2
this is line 3
# sed '1s/this is line/that is line/' test
that is line 1
this is line 2
this is line 3
# sed '2s/this is line/that is line/' test
this is line 1
that is line 2
this is line 3

Members may be able to help you better if you posted a part of the file you are working with and exactly what you are trying to do with the file.

Since the source file and destination file are the same, consider doing something like this.

Input file called inputfile.txt contains just the word 'This'

commandline
perl -pi -w -e 's/This/I did it/g;' inputfile.txt
#This replaces the word This with I did it all from the commandline

inside script called myscript.pl
perl -pi -w -e 's/This/I did it/g;' $1
#Put this perl line inside a script and run it. Passing your input file to the script

Run script from commandline.

#myscript.pl inputfile.txt

-X

thanks for your suggestions. what i ended up doing was just reconstructing the file using a synchronized copy of the original. what i was trying to edit couldn't be done with sed or awk. i tried but it just couldn't be done. so i just used a combination of echo, awk, tee and a variety of other commands to draw out out the wanted line and then appended the rest of the file content to the end of that file