Replacing string in file

Replacing string in file

Post by Subhro S » Tue, 02 Apr 1996 04:00:00



In a file, how could I replace certain strings with others
from the command line?

I.e., I want to replace all instances of "On since" in a file with
"Last login"

I am using ksh.

Thanks!
--

http://www.eecs.nwu.edu/~sksen/                northwestern university
                                                    evanston, illinois

--

http://www.eecs.nwu.edu/~sksen/                northwestern university
                                                    evanston, illinois

 
 
 

Replacing string in file

Post by Martin Wickm » Tue, 02 Apr 1996 04:00:00



>In a file, how could I replace certain strings with others
>from the command line?
>I.e., I want to replace all instances of "On since" in a file with
>"Last login"
>I am using ksh.

Here is a script which changes a string to another. Note that it
works on *all* files in a directory. I guess you can just use:

echo "g/$STRING1/s//$STRING2/gp\nw\nq" | ed -s $FILE

To change it in a single file

---8<-----

#!/bin/ksh

ME=$(basename $0)

STRING1="Find This"
STRING2="Replace with this"

for file in *
do
   if [[ $file != $ME && -f $file ]]
   then
      if grep -q "$STRING1" $file
      then
         echo "Editing file \"$file\":"
         echo "g/$STRING1/s//$STRING2/gp\nw\nq" | ed -s $file
      fi
   fi
done

---8<-----

--

"Windows 95: from the guys who brought you EDLIN"

 
 
 

Replacing string in file

Post by Michael Staa » Tue, 02 Apr 1996 04:00:00



>In a file, how could I replace certain strings with others
>from the command line?

Try this script:
======================================================================
#!/bin/ksh

cmd=`basename $0`

SEPCHAR="~"
EXITV=0
G=""; T=""; RANGE=""

while getopts gtr: name; do
 case $name in
  g) G=g;;
  t) T=t;;
  r) RANGE="$OPTARG";;
  esac
done
shift $(( $OPTIND - 1 ))

# echo Remaining parameters are: "$1,$2,$3,$4,$5"

[ -z "$3" ] && {
        echo "usage: $cmd [-g] [-t] [-r from[,to]] regexp1 regexp2 file [ file ... ]" ; \
        echo "       -g: calls sed with s/re1/re2/g"; \
        echo "       -t: preserve access and modification date (using touch -amr)"; \
        echo "       -r from,to: only replace in range from, to (sed syntax)"; \
        exit 2; \

Quote:}

pat1="$1"
pat2="$2"
shift 2

{ echo "$pat1""$pat2" | grep "$SEPCHAR" > /dev/null ; } && {  \
        echo illegal character in regexps ; \
        exit ; \

Quote:}


        if [ -f "$i" ]; then
                if /bin/mv "$i" "$i~";then
                        /bin/cp -p "$i~" "$i"       # Gaanz wichtig: -p. Sonst ?ndert sich Group und nix geht mehr
                                                # (z.B. bei /etc/se*y/login.cfg und yppasswd...)
                        if sed -e "$RANGE""s$SEPCHAR$pat1$SEPCHAR$pat2$SEPCHAR$G" "$i~" >| "$i" ; then
                                [ -n "$T" ] && touch -amr "$i~" "$i" # Equalize dates (for rdist)
                                cmp -s "$i" "$i~" && {
                                        echo "'$i' not changed"
                                        /bin/mv "$i~" "$i"
                                }
                        else
                                echo "ERROR executing sed, undoing changes" >&2
                                EXITV=1
                                /bin/mv "$i~" "$i"
                        fi
                fi
        else
                echo "not a regular file: $i" >&2
        fi
done

exit $EXITV
======================================================================
--
Michael Staats, Theoretical Physics, Uni-GH Duisburg

<a href="http://WWW.thp.Uni-Duisburg.DE/">Click</a> me!
<a href="http://WWW.thp.Uni-Duisburg.DE/cuaix/cuaix.html">A c.u.aix archive</a>