1829586 Members
2090 Online
109992 Solutions
New Discussion

Inline "Sed" script

 
Sean OB_1
Honored Contributor

Inline "Sed" script

Howdy.

Years ago I had a script that essentially did inline sed on files.

With it you'd call the script and give the sed command, it would then run against the file(s) you specify and do the sed and save the file.

Does anyone have something like this that they'd be willing to post?

TIA.

Sean
5 REPLIES 5
Sanjay_6
Honored Contributor

Re: Inline "Sed" script

Hi Sean,

Is this what you are looking for.

sed one-liner

http://www.student.northpark.edu/pemente/sed/sed1line.txt

Hope this helps.

Regds
Sanjay_6
Honored Contributor

Re: Inline "Sed" script

Rodney Hills
Honored Contributor

Re: Inline "Sed" script

"perl" can do that and more-

perl -p -i -e 's/this/that/g' allmyfiles/*

Will replace the text in all the files under directory allmyfiles.

Change option -i to -i.bu and it will save a backup of each file with an extenstion of .bu.

HTH

-- Rod Hills
There be dragons...
Victor Fridyev
Honored Contributor

Re: Inline "Sed" script

Try this

#!/usr/bin/sh
TMF=/tmp/$$
PRGN=$(basename $0)
if [ $# -ne 3 ]; then
echo "$PRGN:USAGE $PRGN FILENAME CURR CHANGE"
exit
fi
if [ -s $1 ];then
cat $1| sed "s?$2?$3?g" > $TMF
if ! diff $1 $TMF 1>/dev/null ;then
cp $TMF $1
fi
else
echo "$PRGN:File $1 NOT FOUND"
fi
rm $TMF
Entities are not to be multiplied beyond necessity - RTFM
Stuart Abramson
Trusted Contributor

Re: Inline "Sed" script

##
# sed_files.ksh SDA 12/16/03
#
# Change a string in a file and recreate the file with:
#
# string1 -> string2
#
USAGE="Usage: sed_files.ksh [FILE]"
#
# Example:
# sed_files.ksh OLD NEW *.ksh
#

if (( $# < 3 ))
then
print $USAGE
exit 1
fi
OLD=$1
NEW=$2
shift 2
#
for F in $*
do
if [[ -f $F ]]
then
# We have a regular file
file $F | grep -q text
if (( $? == 0 ))
then
# We have a text file
grep -q $OLD $F
if (( $? == 0 ))
then
# We have a text file with "string1" in it
cp -p $F $F.old
sed "s/$OLD/$NEW/g" $F > $F.new
mv $F.new $F
rm $F.old
fi
fi
fi
done