Find and replace a string on remote server


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find and replace a string on remote server
# 1  
Old 03-13-2012
Java Find and replace a string on remote server

Main script command
Code:
ssh  user@server.XYZ.net export "'$ProjectTarget'" < /webdata/ecif/etl/scripts/dbchange.sh

dbchange.sh
Code:
 
#!/bin/bash
for file in find /devl/bfx/ecif/etl/$ProjectTarget/parameterfiles/ -exec grep -l CNTCT{} \;
do ls -l $file
sed -e 's/CNTCT/CNTCTST/' $file > $file.tmp 
mv $file.tmp $file 
done 
for file in find /devl/bfx/ecif/etl/$ProjectTarget/parameterfiles/ -exec grep -l DBCIBNR{} \;
do ls -l $file
sed  's/DBCIBNR/DBCIBC4/g' $file > $file.tmp 
mv $file.tmp $file 
done

This script runs but do not replace the string. Can someone tell me what i am missing here. My local system os is linux and remote system is solaris.
# 2  
Old 03-13-2012
I'm not sure what that syntax is, but I think this may be closer to what you want:

Code:
scp -pq /webdata/ecif/etl/scripts/dbchange.sh  user@server.XYZ.net 
ssh  user@server.XYZ.net  "export ProjectTarget=some_real_name && ./dbchange.sh"

Assumptions - ProejctTarget is a directory on the remote node and the shell script has to live on that node to work. Either that or you have to nfs mount your remote directory on let a local copy of dbchange.sh see it as local.
# 3  
Old 03-13-2012
Quote:
#!/bin/bash
# Script for execution on the Solarix box
for file in find /devl/bfx/ecif/etl/$ProjectTarget/parameterfiles/ -exec grep -l CNTCT{} \;
do ls -l $file
sed -e 's/CNTCT/CNTCTST/' $file > $file.tmp
mv $file.tmp $file
done
for file in find /devl/bfx/ecif/etl/$ProjectTarget/parameterfiles/ -exec grep -l DBCIBNR{} \;
do ls -l $file
sed 's/DBCIBNR/DBCIBC4/g' $file > $file.tmp
mv $file.tmp $file
done
Strongly advise that you copy the script to the remote server and test it thoroughly there from the Solaris command prompt (without the bash shebang unless you have bash on the Solaris box) but with the shebang to match your Solaris account (e.g. /bin/ksh). The script contains numerous syntax errors and fundamental errors and it needs serious debugging. When debugged I would recommend that you execute the remote script from ssh.
As far as I can see the script actually does nothing useful - mainly because the "find" statements never execute. Unless this is a mispaste, the script should be giving syntax errors on the "for" and "do" lines.

My usual comment. With faulty code we need a written description of what the script should do complete with sample input and sample output.



My guess for some corrections (assuming that you run the script on the Solaris box). Totally untested and without warranty and intended as an exercise in correcting Shell syntax:
Code:
#!/bin/ksh
#
Project_Target="MYVALUE"      # This variable needs a value. Perhaps as $1 ?
#
find /devl/bfx/ecif/etl/"${ProjectTarget}"/parameterfiles/ -exec grep -l "CNTCT" {} \; | while read file
do
     ls -l "${file}"
     sed -e 's/CNTCT/CNTCTST/g' "${file}" > "${file}.tmp"
     mv "${file}.tmp" "${file}" 
done
# 
find /devl/bfx/ecif/etl/"${ProjectTarget}"/parameterfiles/ -exec grep -l "DBCIBNR" {} \; | while read file
do
     ls -l "${file}"
     sed -e 's/DBCIBNR/DBCIBC4/g' "${file}" > "${file}.tmp" 
     mv "${file}.tmp" "${file}"
done

Notice the subtle addition of space characters on the "grep" lines.

Because the script now has the potential to change live parameter files, please test on a test system having backed-up the original files.
A script of this nature should backup the original files before making changes. It's good working practice and it creates an Audit Trail.

Last edited by methyl; 03-13-2012 at 07:25 PM.. Reason: remove as many syntax errors as possible. spacing on the grep line. layout and indentation. quoting paranoia! correct sed
# 4  
Old 03-14-2012
I copied the script to the remote server and tested and it worked fine code is
Code:
#!/bin/ksh
find /devl/bfx/ecif/etl/visibility/parameterfiles/ -exec grep -l CNTCT {} \; | while read file
do ls -l $file
sed -e 's/CNTCT/CNTCTST/' $file> $file.tmp
mv $file.tmp $file
done
find /devl/bfx/ecif/etl/visibility/parameterfiles/ -exec grep -l DBCIBNR {} \; | while read file
do ls -l $file
sed  's/DBCIBNR/DBCIBC4/g' $file> $file.tmp
mv $file.tmp $file
done

when i tried the same from local i could not get the desired output.
main Code
Code:
#!/bin/bash
echo "Today is $(date)"
echo " This script will copy Prod Unix code into Breakfix Env."
echo "Choose Project: (1-18)"
echo " 1- CAM Extracts"
echo " 2- CASNCOA Extracts"
echo " 3- CDC Extracts"
echo " 4- CTMNZ Extracts"
echo " 5- CYBERLIFE  Extracts"
echo " 6- DW_ASCII_Conversion"
echo " 7- ECIF2CIFLOW"
echo " 8- ECIFNZ Extracts"
echo " 9- KEYNOTE Extracts"
echo " 10- NRS Extracts "
echo " 11- VANTAGE Extracts"
echo " 12- Prospects Extracts"
echo " 13- Visibility Extracts"
echo " 14- DW Extracts"
echo " 15- CASS MASN"
echo " 16- NWBank"
echo " 17- EASE"
echo " 18- Email Harvest"
echo " 19- CIM Stats Extract"
echo " 20- Change Identifier"
echo " 21- OFAC Extract"
echo " 22- Common Objects"
echo " 23- PLB Prefix Extract"
echo " 24- EDelivery Extract"
echo " 25- Farm Bureau Extract"
echo " 26- CNT Harvest"
echo " 27- AKM2ECIF"
echo " 28- CTM Claim Account Load"
echo " 29- Batch Processor"
echo " 30- Unload2MF"
echo " 31- ECIF_BATCHSTAT"
echo " 32- ALSEXTRACT"
echo " 33- SUPPORT"

echo "  ************************************"
echo ""
read Project
export Project1=$Project
case $Project in
        1) export Project=CAM_Extracts
	export ProjectTarget=camextract
        ;;
        2) export Project=casncoaext
	export ProjectTarget=casncoaext
        ;;
        3) export Project=cdcextract
	export ProjectTarget=cdcextract
        ;;
        4) export Project=ctmnzextract
	export ProjectTarget=ctmnzext
        ;;
        5) export Project=CYBERLIFE_EXTRACT
	export ProjectTarget=cyberlife
        ;;
        6) export Project=DW_ASCII_Conversion
	export ProjectTarget=dwascconv
        ;;
        7) export Project=ECIF2CIFLOW
	export ProjectTarget=ecif2ci
        ;;
        8) export Project=ecifnzext
	export ProjectTarget=ecifnzext
        ;;
        9) export Project=KEYNOTE_EXTRACT
	export ProjectTarget=keynote
        ;;
        10) export Project=NRS_EXTRACT
	export ProjectTarget=nrsextract
        ;;
        11) export Project=VANTAGE_EXTRACT
	export ProjectTarget=vantage
        ;;
        12) export Project=PROSPECTS_EXTRACT
	export ProjectTarget=prospects
        ;;
        13) export Project=visibility
	export ProjectTarget=visibility
        ;;
        14) export Project=dwextract
	export ProjectTarget=dwextract
        ;;
        15) export Project=cassmasn
	export ProjectTarget=cassmasn
        ;;
        16) export Project=nwbank
	export ProjectTarget=nwbank
        ;;
        17) export Project=ease
	export ProjectTarget=ease
        ;;
        18) export Project=emailharvest
	export ProjectTarget=emailharvest
        ;;
        19) export Project=cimstatsextract
	export ProjectTarget=cimstatsextract
        ;;
        20) export Project=changeid
	export ProjectTarget=changeid
        ;;
        21) export Project=ofacextract
	export ProjectTarget=ofacextract
        ;;
        22) export Project=common_objects
	export ProjectTarget=common_objects
        ;;
        23) export Project=plb_prefix_extract
	export ProjectTarget=plbextract
        ;;
        24) export Project=edelivery_extract
	export ProjectTarget=edelextrct
        ;;
        25) export Project=farm_bureau_extract
	export ProjectTarget=farm_bureau_extract
        ;;
        26) export Project=cntharvest
	export ProjectTarget=cntharvest
        ;;
        27) export Project=akm2ecif
	export ProjectTarget=akm2ecif
        ;;
        28) export Project=ctmclactld
	export ProjectTarget=ctmclactld
        ;;
        29) export Project=batchproc
	export ProjectTarget=batchproc
        ;;
        30) export Project=unload2mf
	export ProjectTarget=unload2mf
        ;;
	31) export Project=ECIF_BATCHSTAT
	export ProjectTarget=batchstats	
        ;;
	32) export Project=ALSEXTRACT
	export ProjectTarget=alsextract 
	;;
	33) export Project=support
	export ProjectTarget=support
        ;;
esac
case $Project1 in
        1) export Project=CAM_Extracts
	export ProjectTarget=camextract
	scp  ecifetls@boom.nwie.net:/vol/ecif/etl/$ProjectTarget/bin/*.* ecifetls@boom.nwie.net:/devl/bfx/ecif/etl/$ProjectTarget/bin/
	scp  ecifetls@boom.nwie.net:/vol/ecif/etl/$ProjectTarget/scriptfiles/*.* ecifetls@boom.nwie.net:/devl/bfx/ecif/etl/$ProjectTarget/scriptfiles/
	scp  ecifetls@boom.nwie.net:/vol/ecif/etl/$ProjectTarget/parameterfiles/*.* ecifetls@boom.nwie.net:/devl/bfx/ecif/etl/$ProjectTarget/parameterfiles/
	scp  ecifetls@boom.nwie.net:/vol/ecif/etl/$ProjectTarget/sql/*.* ecifetls@boom.nwie.net:/devl/bfx/ecif/etl/$ProjectTarget/sql/
	scp  ecifetls@boom.nwie.net:/vol/ecif/etl/$ProjectTarget/maestro/*.* ecifetls@boom.nwie.net:/devl/bfx/ecif/etl/$ProjectTarget/maestro/
	scp  ecifetls@boom.nwie.net:/vol/ecif/etl/$ProjectTarget/syncsort/*.* ecifetls@boom.nwie.net:/devl/bfx/ecif/etl/$ProjectTarget/syncsort/
	ssh  ecifetls@boom.nwie.net export "'$ProjectTarget'" < /webdata/ecif/etl/scripts/dbchange.sh ;;
esac

dbchange.sh
Code:
#!/bin/ksh

find /devl/bfx/ecif/etl/"${ProjectTarget}"/parameterfiles/ -exec grep -l "CNTCT" {} \; | while read file
do
     ls -l "${file}"
     sed -e 's/CNTCT/CNTCTST/g' "${file}" > "${file}.tmp"
     mv "${file}.tmp" "${file}" 
done
# 
find /devl/bfx/ecif/etl/"${ProjectTarget}"/parameterfiles/ -exec grep -l "DBCIBNR" {} \; | while read file
do
     ls -l "${file}"
     sed -e 's/DBCIBNR/DBCIBC4/g' "${file}" > "${file}.tmp" 
     mv "${file}.tmp" "${file}"
done

Here i m trying to copy file from Server1 Dir-1 to Server1 Dir-2 from Server2 and finding and replacing some string in parameter dir-2 of server1 .
# 5  
Old 03-19-2012
Do anyone has any suggestions ????
# 6  
Old 03-19-2012
Any suggestions???
# 7  
Old 03-19-2012
1) Bumping up posts or double posting is not permitted in these forums.
2) You say you did mot get desired output, fine, what did you get then?
3) Why are you mixing scripts written in bash and ksh? knowing that linux will not understand by default ksh and solaris bash...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script connect to remote server, not find files and exit only from remote server, but not from scrip

I have a script, which connecting to remote server and first checks, if the files are there by timestamp. If not I want the script exit without error. Below is a code TARFILE=${NAME}.tar TARGZFILE=${NAME}.tar.gz ssh ${DESTSERVNAME} 'cd /export/home/iciprod/download/let/monthly;... (3 Replies)
Discussion started by: digioleg54
3 Replies

2. UNIX for Dummies Questions & Answers

Find and Replace string in UNIX

Hi All, Greetings. I have a .dat file which somewhere in its content contains symbol ""^ I want to replace it with "^ I tried with SED command but could not achieve what i wanted sed -e "s/'""^'/'"^'/ig" filename.dat (5 Replies)
Discussion started by: MaddyS
5 Replies

3. Shell Programming and Scripting

Find the remote server status.

Hi All, I would like to connect from "Instance A" to "Instance B" with the help of sftp. Where as Instance B is having clustered servers ( 2 servers pointing same instance ). Now, my question is before connecting to "Instance B" from "Instance A" how do know whether server is running or not.... (3 Replies)
Discussion started by: Girish19
3 Replies

4. Solaris

How to find and replace a string?

Dear All I need to find and replace a string in a set of files. I try as : #find / -name "*"|xargs grep "Tektra"|grep -v "Tektra GSM BTS" But it doesn't work. It just finds the string in the files. I need to find and replace it.Can you please let me know how to correct it? Thank you (2 Replies)
Discussion started by: hadimotamedi
2 Replies

5. Shell Programming and Scripting

HPUX find string in directory and filetype and replace string

Hi, Here's my dilemma. I need to replace the string Sept_2012 to Oct_2012 in all *config.py files within the current directory and below directories Is this possible? Also I am trying to find all instances of the string Sept_2012 within files in the current directory and below I have... (13 Replies)
Discussion started by: pure_jax
13 Replies

6. Shell Programming and Scripting

find string and replace with string in other file

Dear all, I need your help, I have file like this: file1:23456 01910964830098775635 34567 01942809546554654323 67589 26546854368698023653 09778 58716868568576876878 08675 86178546154065406546 08573 54165843543054354305 . .file2: 23456 25 34567 26 67589 27 (2 Replies)
Discussion started by: attila
2 Replies

7. Shell Programming and Scripting

Find the position of a string and replace with another string

Hi, I have a file named "Test_2008_01_21" The file contains a string "manual" that occurs many times in the file How can i find the positions of the string "manual" in the file Ex: if the string " manual " occurs three times in the file. i want to replace the second occurance of string... (6 Replies)
Discussion started by: bab123
6 Replies

8. UNIX for Dummies Questions & Answers

Find and replace character in a string

Hi all, My problem is the following: I've a script that must list all files in a directory and write this information in a text file. I've tried to get the list through ls command and then write it using msgecho msgecho "`ls $PATH_APS_JOB_ORA`" This works good but the created string... (7 Replies)
Discussion started by: callimaco0082
7 Replies

9. Shell Programming and Scripting

Find files not accessed on a remote server and delete - Help!

Hi Guys, I am currently working on a script to find all the files that have not been accessed for the past 2 years. This, i guess has been discussed n number of times in this forum. Now, my requirement is to find all the files in the remote windows server. I have it mounted in unix. I was... (1 Reply)
Discussion started by: bond_bhai
1 Replies

10. Programming

how to find and replace string

hi I wanted to find this char " ^M " in my file and replace it with blank space. I am using Unix system. If i give command " :%s/^M//gc " it wont work so can anyone tell what is command to find and replace thankx (3 Replies)
Discussion started by: mridula
3 Replies
Login or Register to Ask a Question