Operating System - HP-UX
1827974 Members
2887 Online
109973 Solutions
New Discussion

replace a specific line in a file?

 
SOLVED
Go to solution
Russ Hancock_1
Frequent Advisor

replace a specific line in a file?

Anyone know how to replace an entire line ( say line 40 ) with a complete new line ?!

The line to be replaced will not have the same content everytime, but the line number will be constant....

Thanks again(!)
Russ.
Russ
5 REPLIES 5
Heiner E. Lennackers
Respected Contributor

Re: replace a specific line in a file?

In a script?

ed filename <40s/^.*$/New Content/
w
q
EOI



Heiner
if this makes any sense to you, you have a BIG problem
Pete Randall
Outstanding Contributor

Re: replace a specific line in a file?

Russ,

On quick review I didn't see the exact solution you're looking for, but I'm attaching the "Handy One-liners for Sed" for your future reference (orginally courtesy of Princess Paula).

Pete

Pete
James R. Ferguson
Acclaimed Contributor
Solution

Re: replace a specific line in a file?

Hi Russ:

Try:

# sed -e 40's/.*/new stuff on line/' filein > fileout

Regards!

...JRF...
Leif Halvarsson_2
Honored Contributor

Re: replace a specific line in a file?

Hi
something like this


rep_line "YOUR NEW LINE" file2




newline=$1
let line=1
while read line
do
if [ $line -eq 40 ]
then
echo $newline
else
echo $line
fi
let line =$line + 1
done
Leif Halvarsson_2
Honored Contributor

Re: replace a specific line in a file?

sorry, line counter got wrong.

Hi
something like this


rep_line "YOUR NEW LINE" file2




newline=$1
let n=1
while read line
do
if [ $n -eq 40 ]
then
echo $newline
else
echo $line
fi
let n =$n + 1
done