Operating System - HP-UX
1819791 Members
3451 Online
109607 Solutions
New Discussion юеВ

SED script to edit file in place

 
SOLVED
Go to solution
Sean OB_1
Honored Contributor

SED script to edit file in place

Hello!

I'm looking for a script to run sed against a file and save the file as the same name.

I once had a utility that did this but I can't find it now.

I have about 1000 files that I need to edit and with this utility I could run it something like this:

sedutil s/arg1/arg2/g *

and it would run against all files and modify the ones that needed it and save them with the same file name.

Does anyone have something like this they could send me?

TIA

Sean
7 REPLIES 7
Steven E. Protter
Exalted Contributor
Solution

Re: SED script to edit file in place

This script does it, it creates a filelist and then edits the files with sed commands shelling out of perl.

Read it carefully its extremely powerful.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Sean OB_1
Honored Contributor

Re: SED script to edit file in place

Now if I can figure out how to have IE save this as a text file instead of binary!
Jean-Louis Phelix
Honored Contributor

Re: SED script to edit file in place

Hi,

Also a simple one in shell :

#!/usr/bin/sh
SEDEXP="$1"
shift
TMP=/tmp/file$$
for FILE
do
sed "$SEDEXP" $FILE > $TMP
mv $TMP $FILE
done

Use it as :

$ sedutil "s/arg1/arg2/g" file1 file2 ...

Regards.
It works for me (┬й Bill McNAMARA ...)
Graham Cameron_1
Honored Contributor

Re: SED script to edit file in place

sed cannot update a file in place, it has to create a new copy. Nothing to then stop you plonking the new copy over the original though...

for f in *
do
sed "s/arg1/arg2/g" $f > F && mv F $f
done

-- Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
Sean OB_1
Honored Contributor

Re: SED script to edit file in place

Thanks everyone.
H.Merijn Brand (procura
Honored Contributor

Re: SED script to edit file in place

as the others have shown, sed needs a separate step to do this. perl does not

# perl -pi -e's/arg1/arg2/g' file

in-place changes arg1 to arg2 (not checking on word bounds, so arg11 will be changed to arg21

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
curt larson_1
Honored Contributor

Re: SED script to edit file in place

i'll add a bit to this

#!/usr/bin/sh
origDir=$(pwd)
SEDEXP="$1"
shift
TMP=/tmp/file$$

function fileops {
typeset FILE=$1

if [[ ! -r $file ]] ;then
print "$(pwd)/$file isn't readable"
return
fi
if [[ ! -w $file ]] ;then
print "$(pwd)/$file isn't writable"
return
fi

sed "$SEDEXP" $FILE > $TMP
mv $TMP $FILE
}

for arg in $@
do
if [[ -d $arg ]] ;then
cd $arg
for i in ls
do
if [[ -f $i ]] ;then
fileops $i
else
print "$(pwd)/$i isn't a regular file"
fi
done
cd $origDir
elif [[ -f $arg ]] ;then
fileops $arg
else
print "$arg isn't a directory or regualar file"
fi
done