Lets assume that the given awk
command
awk 'NR==1,/mode/{next}/tot/,NR==0{next}{print}' 1.out > 2.dat
was executed, and created file 2.dat
.
In order to modify an existing file 3.out
as requested, here is the answer using one awk
command (to create a temporary file /tmp/3.out.tmpfile
):
awk 'FNR==1{nf++}; NR==1,/mode/{print;next}; f2==0{f2=1;nextfile}; nf==2{print;next}; /tot/,0{print}' 3.out 2.dat 3.out > /tmp/3.out.tmpfile
and one mv
command (to replace the original 3.out
file by the modified one /tmp/3.out.tmpfile
):
mv /tmp/3.out.tmpfile 3.out
For example, if the original 3.out
file contains:
line-1
line-2
mode
line-X
line-Y
line-Z
tot
line-3
line-4
line-5
and file 2.dat
contains:
inserted-line-A
inserted-line-B
inserted-line-C
inserted-line-D
inserted-line-E
then the above proposed commands will result in a modified 3.out
file that now contains:
line-1
line-2
mode
inserted-line-A
inserted-line-B
inserted-line-C
inserted-line-D
inserted-line-E
tot
line-3
line-4
line-5
The original 3.out
lines between mode
and tot
have now been replaced by the ones in file 2.dat
.