replace file's content between specific line numbers with another file using BASH

1

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

Posted 2014-02-07T09:23:36.773

Reputation: 145

Answers

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

Posted 2014-02-07T09:23:36.773

Reputation: 23 855

...and today I learn that ed & sed have the r command. Ouch. – user1686 – 2014-02-08T10:10:33.740

2

start=2
end=3

{
  head -n $((start-1)) old.txt
  cat new.txt
  tail -n $((end+1)) old.txt
} > merged.txt

user1686

Posted 2014-02-07T09:23:36.773

Reputation: 283 655

I prefer this style, for readability. But looks like the tail -n $((end+1)) old.txt is not correct. You have to read in the number of lines of the file, and subtract your end position. tail -n $(($(wc -l < old.txt)-end)) old.txt – Marshall – 2017-01-02T23:53:59.430