В программу входят две части: получение нужного вам результата, а затем замена содержимого исходного файла на этот вывод:
#!/bin/sh
# output the first five lines of the first argument
# followed by all but the first of the second argument
# if successful, replace the second argument with the
# result
# cribbed almost entirely from Kernighan & Pike's
$ "The Unix Programming Environment" script "overwrite"
case $# in
0|1) echo 'Usage: replace5 filea fileb' 1>&2; exit 2
esac
filea=$1; fileb=$2
new=/tmp/$$.new; old=/tmp/$$.old
trap 'rm -f $new; exit 1' 1 2 15 # clean up files
# collect input
if head -5 $filea >$new && tail -n +2 $fileb >> $new
then
cp $filea $old # save original file
trap 'trap "" 1 2 15; cp $filea $old # ignore signals
rm -f $new $old; exit 1' 1 2 15 # during restore
cp $new $filea
else
echo "replace5: failed, $filea unchanged" 1>&2
exit 1
fi
rm -f $new $old