FW: [Gllug] Awk Sed
Pete Ryland
pdr at pdr.cx
Fri Dec 7 09:55:01 UTC 2001
On Fri, Dec 07, 2001 at 07:39:52AM +0000, harry wrote:
> Hi all
>
> Having had a little play I came up with the following to run through a
> directory and change text in only the files found.
>
> Put this in a shell script called harrysed
> #! /bin/sh
> for file in $(ls $3);do
>
> echo $file
> sed -e 's/'$1'/'$2'/g' $file > '$fileff'; more '$fileff' > $file; rm
> '$fileff'
>
> done
>
> Run like this
>
> >$./harrysed a b *.txt
>
> This will go through the current directory and substitute all the a's for b's
> only where the file has the .txt extension. Again it has not been tested much
> but it seems to work Ok. Any comments warmly welcomed.
some quick suggestions (if you must use sed :)
#!/bin/sh
if [ -z $3 ]; then
echo "Usage: $0 a b files"
exit
fi
pat1=$1
pat2=$2
shift 2
for file in $*; do
if [ -d $file ]; then
echo "$0: ignoring directory $file!"
continue
fi
if [ ! -r $file ]; then
echo "$0: $file is not readable!"
continue
fi
sed -e "s/$pat1/$pat2/g" $file > ".harrytemp$file" && mv ".harrytemp$file" $file
rm -f ".harrytemp$file"
done
Better yet, for more flexibility, make the user specify the whole sed
expression, similar to the rename(1) command.
Pete
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 240 bytes
Desc: not available
URL: <http://mailman.lug.org.uk/pipermail/gllug/attachments/20011207/b7a111b6/attachment.pgp>
More information about the GLLUG
mailing list