I'd probably use sed
for file in bar baz bam; do
newfile=$(mktemp)
(sed '5q' $file; sed -n '6,15p' foo; sed '1,15d' $file) > $newfile
mv -f $newfile $file
done
or Perl
#!/usr/bin/env perl
use v5.6; # lexical filehandles need Perl >= 5.6
# usage: $0 foo bar baz bam
# replaces lines 4-15 of qw(bar baz bam) with lines 6-15 of foo
my @ins;
{
open my $fh, shift;
while (<$fh>) {
push @ins, $_ if 6 .. 15;
last if $. >= 15;
}
}
$^I = '';
while (<>) {
print if not 5 .. 15;
print @ins if $. == 4;
}
continue {
close ARGV if eof ARGV;
}
but ed
works too.
echo '6,15wq foo-part' | ed foo
for i in bar baz bam; do
echo -e '5,15d\n4r foo-part\nwq' | ed $i
done
rm -f foo-part