1

What is the most efficient way to replace file's content between specific line numbers with another file?

Here is a sample:

main.txt:

a b c
d e f
g h i
j k l

new.part.txt

x y z
p q r
s t u

The block of text starting at line 2 and ending at line 3 of main.txt is replaced with new.part.txt. The desired file is as follow:

a b c
x y z
p q r
s t u
j k l

This answer is for the case when range of the desired block is defined via marker strings. I need a solution that uses line numbers for defining range of the desired block.

Kadir
  • 145

2 Answers2

2
start=2
end=3

{
  head -n $((start-1)) old.txt
  cat new.txt
  tail -n $((end+1)) old.txt
} > merged.txt
grawity
  • 501,077
2

You don't use bash at all.

The Bourne Again shell is a shell. The actual utility programs for doing this are not part of the shell, and are largely shell-agnostic. grawity's answer uses shell features, but mainly for parameterization.

Here's how to do it with ex, editing the file in place:

ex main.txt << EOT
2,3d
1r new.part.txt
w
EOT

Here's how one does it with sed, writing the changed data to standard output:

sed -e '1r new.part.txt' -e '2,3d' main.txt
JdeBP
  • 27,556
  • 1
  • 77
  • 106