Tag Archives: sed ecentrix

REPLACE STRING IN MULTIPLE FILE ON DIRECTORY


Berikut adalah sedikit shell script untuk me-replace string di beberapa file or directory.

[root@node1 sort]# vim replaceString.sh
#!/bin/bash

writeLog(){
        LOG="/tmp/logReplaceString-$( date +%F)"
        echo $1
        echo [ $(date +%r) ] $1 >> $LOG
}

replaceString(){
LOG="/tmp/replaceString-$( date +%F)"
        if [ $# -ne 3 ]; then
                writeLog ""
                writeLog "---------------------------------------------------------------------------"
                writeLog "Wrong parameter!!!"
                writeLog "Format Parameter : [Full Path Directory] [TEXT TO SEARCH] [TEXT TO REPLACE]"
                writeLog "Example          : /home/ecentrix/cfg/ centrix gmosecentrix"
                writeLog "---------------------------------------------------------------------------"
        else
                if [ -d "$1" ] || [ -f "$1" ];then
                        egrep -wlRZ "$2" $1 | xargs -0 -l sed -i -e "s/$2/$3/g"
                        writeLog ""
                        writeLog "-------------------------------------------------------------------"
                        writeLog "Replace word $2 to $3 on directory $1"
                        writeLog "-------------------------------------------------------------------"
                        writeLog ""
                else
                        writeLog $1 " is not file or directory"
                fi
        fi
}

replaceString $1 $2 $3

Cara penggunaan:
[root@node1 sort]# sh /home/ecentrix/sort/replaceString.sh /home/ecentrix/cfg/ ecentrix gmosecentrix
artinya setiap kata "ecentrix" yang ditemukan di directory "/home/ecentrix/cfg" akan di replace dengan kata
gmosecentrix"
Biasanya ini digunakan untuk pergantian IP atau password di file2 configurasi atau di file2 php
atau bisa di buat di cronta
[root@node1 sort]# crontab -l
59 23 * * * /home/ecentrix/sort/replaceString.sh /home/ecentrix/cfg/ 172.10.10.0 172.10.10.10
59 23 * * * /home/ecentrix/sort/replaceString.sh /home/ecentrix/cfg/ ecentrix new-ecentrix

Semoga bermanfaat :)
shisdew

Listens until think alike

moses.spaceku@yahoo.com / voip ipbx

Hosted PBX, IP-PBX SOHO/ CALL CENTER, VOICE GATEWAY, VOICE CARD, COST EFECTIVE SOLUTIONS (LCR), GSM/CDMA GATEWAY

%d bloggers like this: