[YLUG] How do to substition in a file with sed or similar

Charles Forsyth forsyth at terzarima.net
Fri Oct 27 11:56:46 BST 2006


see the `overwrite' command in Kernighan and Pike:

	overwrite myfile sed 's/fool/bar' myfile
and it's useful for tr and commands with similar interfaces

it also won't overwrite the output if the command fails (non-zero exit status),
which is often useful on its own.

it's just a shell script.  i've just typed in the
following (i don't include their changing $PATH), and there might be typing errors.


#!/bin/sh

case $# in
0|1) echo 'usage: overwrite file cmd [args ...]' >&2; exit 2
esac

file=$1; shift
nwe=/tmp/overwr1.$$; old=/tmp/overwr2.$$
trap 'rm -f $new $old; exit 1' 1 2 15		# clean up files

if "$@" >$new	# collect input
then
	cp $file $old	# save original file
	trap '' 1 2 15	# we are committed; ignore signals
	cp $new $file
else
	echo "overwrite: $1 failed, $file unchanged" >&2
	exit 1
fi
rm -f $new $old
-------------- next part --------------
An embedded message was scrubbed...
From: "Robert Hulme" <rob at robhulme.com>
Subject: [YLUG] How do to substition in a file with sed or similar
Date: Fri, 27 Oct 2006 11:47:56 +0100
Size: 4091
Url: http://mailman.lug.org.uk/pipermail/york/attachments/20061027/e987297c/attachment.mht


More information about the York mailing list