Shell Script to replace tokens in multiple files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell Script to replace tokens in multiple files
# 15  
Old 10-29-2008
Inside @USER_HOME@ will be a directory, and inside @BEA_INSTALL_DIR@ will be a directory. The rest will just be a combo of letters and numbers
# 16  
Old 10-29-2008
Still one question left unanswered,

Quote:
- An exact sample of how the variables will look like after the replacement, especially for variables with multiple tokens ?
example,
Code:
SERVER_NAME="@ADMIN_SERVER_NAME@"   --->                      SERVER_NAME="server A"                               
SERVER_URL="t3://@UNIX_SERVER@:@ADMIN_SERVER_PORT@"     --->    SERVER_URL="t3://Server UNIX:1001"

I'm assuming the headers that you posted, are exactly as they look in of all of the files, and there are no more of them (?).

And when you enter the variable values at the prompt, do you use quotes around them ( say "server A" ), or simply:

Code:
enter SERVER_NAME:

server A

# 17  
Old 10-29-2008
Quote:
Originally Posted by rubin
Still one question left unanswered,



example,
Code:
SERVER_NAME="@ADMIN_SERVER_NAME@"   --->                      SERVER_NAME="server A"                               
SERVER_URL="t3://@UNIX_SERVER@:@ADMIN_SERVER_PORT@"     --->    SERVER_URL="t3://Server UNIX:1001"

I'm assuming the headers that you posted, are exactly as they look in of all of the files, and there are no more of them (?).

And when you enter the variable values at the prompt, do you use quotes around them ( say "server A" ), or simply:

Code:
enter SERVER_NAME:

server A

I have not been using quotes when typing in the values.

The one will look like

SERVER_URL="t3://servername:9999"

There are many more scripts, but those are the only real distinct headers, the rest are just simple one variable, one tag.
# 18  
Old 10-29-2008
The code is tested with the data you posted here and it works fine. When you provide the missing info ( see below ) I'll update the post.

Code:
#!/bin/bash
#set -x

cd /path/to/files/

read -p "MAN_SERVER_NAME: "   v1
read -p "BEA_INSTALL_DIR: "   v2
read -p "BASE_DOMAIN_NAME: "  v3
read -p "DOMAIN_NAME: "       v4
read -p "ADMIN_SERVER_NAME: " v5
read -p "UNIX_SERVER: "       v6
read -p "ADMIN_SERVER_PORT: " v7
read -p "ADMIN_UNIX_SERVER: " v8
read -p "USER_HOME: "         v9
read -p "KEYS_SUB_DIR: "      v10
read -p "MAN_SERVER_PORT: "   v11
read -p "WLS_ENV: "           v12
read -p "WEBLOGIC_VERSION: "  v13

#read -p "/: "  path

#If you want the path hardcoded use,
path="/path/to/tmp/dir/"


for file in *WLS

 do

  awk -F@  '
	    /@MAN_SERVER_NAME@/   			     {$0=$1 v1 $3}
            /@BEA_INSTALL_DIR@/   			     {$0=$1 v2 $3}
            /@BASE_DOMAIN_NAME@/  			     {$0=$1 v3 $3}
            /@DOMAIN_NAME@/       			     {$0=$1 v4 $3}
            /@ADMIN_SERVER_NAME@/ 			     {$0=$1 v5 $3}
            /@UNIX_SERVER@/ && /@ADMIN_SERVER_PORT@/         {$0=$1 v6 $3 v7 $5}
            /@ADMIN_UNIX_SERVER@/			     {$0=$1 v8 $3 v7 $5}
            /@USER_HOME@/  && /@KEYS_SUB_DIR@/               {$0=$1 v9 $3 v3 $5 v10 $7}     	        
             # missing in headers -> /@MAN_SERVER_PORT@/     {$0=...v11...}
             # missing in headers -> /@WLS_ENV@/             {$0=...v12...}
            /@WEBLOGIC_VERSION@/  			     {$0=$1 v13 $3}

 1' v1="$v1" v2="$v2" v3="$v3" v4="$v4" v5="$v5" v6="$v6" v7="$v7" v8="$v8" v9="$v9" v10="$v10" v11="$v11" v12="$v12" v13="$v13" "$file" > "$path"/"$file"



 done

# 19  
Old 10-31-2008
Quote:
Originally Posted by rubin
The code is tested with the data you posted here and it works fine. When you provide the missing info ( see below ) I'll update the post.

Code:
#!/bin/bash
#set -x

cd /path/to/files/

read -p "MAN_SERVER_NAME: "   v1
read -p "BEA_INSTALL_DIR: "   v2
read -p "BASE_DOMAIN_NAME: "  v3
read -p "DOMAIN_NAME: "       v4
read -p "ADMIN_SERVER_NAME: " v5
read -p "UNIX_SERVER: "       v6
read -p "ADMIN_SERVER_PORT: " v7
read -p "ADMIN_UNIX_SERVER: " v8
read -p "USER_HOME: "         v9
read -p "KEYS_SUB_DIR: "      v10
read -p "MAN_SERVER_PORT: "   v11
read -p "WLS_ENV: "           v12
read -p "WEBLOGIC_VERSION: "  v13

#read -p "/: "  path

#If you want the path hardcoded use,
path="/path/to/tmp/dir/"


for file in *WLS

 do

  awk -F@  '
	    /@MAN_SERVER_NAME@/   			     {$0=$1 v1 $3}
            /@BEA_INSTALL_DIR@/   			     {$0=$1 v2 $3}
            /@BASE_DOMAIN_NAME@/  			     {$0=$1 v3 $3}
            /@DOMAIN_NAME@/       			     {$0=$1 v4 $3}
            /@ADMIN_SERVER_NAME@/ 			     {$0=$1 v5 $3}
            /@UNIX_SERVER@/ && /@ADMIN_SERVER_PORT@/         {$0=$1 v6 $3 v7 $5}
            /@ADMIN_UNIX_SERVER@/			     {$0=$1 v8 $3 v7 $5}
            /@USER_HOME@/  && /@KEYS_SUB_DIR@/               {$0=$1 v9 $3 v3 $5 v10 $7}     	        
             # missing in headers -> /@MAN_SERVER_PORT@/     {$0=...v11...}
             # missing in headers -> /@WLS_ENV@/             {$0=...v12...}
            /@WEBLOGIC_VERSION@/  			     {$0=$1 v13 $3}

 1' v1="$v1" v2="$v2" v3="$v3" v4="$v4" v5="$v5" v6="$v6" v7="$v7" v8="$v8" v9="$v9" v10="$v10" v11="$v11" v12="$v12" v13="$v13" "$file" > "$path"/"$file"



 done



Code:
# [MAKE CHANGES TO THIS SECTION ONLY] ==================================]
# [VARIABLES -----------------------------------------------------------]

SERVER_NAME="@MAN_SERVER_NAME@"
LISTEN_PORT="@MAN_SERVER_PORT@"
UNIX_SERVER_NAME="@UNIX_SERVER@"
BASE_DIR="@BEA_INSTALL_DIR@"
DOMAIN_BASE="${BASE_DIR}/user_projects/@BASE_DOMAIN_NAME@"
DOMAIN_DIR="${DOMAIN_BASE}/@DOMAIN_NAME@"
MANSERVER_DIR="${DOMAIN_BASE}/managedservers"
KEYS_DIR="@USER_HOME@/keys/@BASE_DOMAIN_NAME@/@KEYS_SUB_DIR@"

# [DO NOT MAKE ANY CHANGES BELOW THIS LINE] ============================]
# [CONSTANTS -----------------------------------------------------------]

DOMAIN_NAME="@DOMAIN_NAME@"
PID_FILE="${SERVER_DIR}/server_${SERVER_NAME}.pid"
SERVER_DIR="${MANSERVER_DIR}/${SERVER_NAME}"
SERVER_URL="t3://${UNIX_SERVER_NAME}:${LISTEN_PORT}"
ADMIN_URL="t3://@ADMIN_UNIX_SERVER@:@ADMIN_SERVER_PORT@"
AUDIT_FILE="${DOMAIN_DIR}/logs/audit.txt"
PY_NAME=".forceshutdown.py"
WL_KEY_FILE="${KEYS_DIR}/system-WLKey.secure"
WL_CONFIG_FILE="${DOMAIN_DIR}/system-WLConfig.secure"
WLS_ENV="@WLS_ENV@"

Code:
# [MAKE CHANGES TO THIS SECTION ONLY] ==================================]
# [VARIABLES -----------------------------------------------------------]

SERVER_NAME="@MAN_SERVER_NAME@"
LISTEN_PORT="@MAN_SERVER_PORT@"
UNIX_SERVER_NAME="@UNIX_SERVER@"
BASE_DIR="@BEA_INSTALL_DIR@"
DOMAIN_BASE="${BASE_DIR}/user_projects/@BASE_DOMAIN_NAME@"
DOMAIN_DIR="${DOMAIN_BASE}/@DOMAIN_NAME@"
MANSERVER_DIR="${DOMAIN_BASE}/managedservers"
KEYS_DIR="@USER_HOME@/keys/@BASE_DOMAIN_NAME@/@KEYS_SUB_DIR@"

# [DO NOT MAKE ANY CHANGES BELOW THIS LINE] ============================]
# [CONSTANTS -----------------------------------------------------------]

DOMAIN_NAME="@DOMAIN_NAME@"
PID_FILE="${SERVER_DIR}/server_${SERVER_NAME}.pid"
SERVER_DIR="${MANSERVER_DIR}/${SERVER_NAME}"
SERVER_URL="t3://${UNIX_SERVER_NAME}:${LISTEN_PORT}"
ADMIN_URL="t3://@ADMIN_UNIX_SERVER@:@ADMIN_SERVER_PORT@"
AUDIT_FILE=${DOMAIN_DIR}/logs/audit.txt
PY_NAME=".shutdown.py"
WL_KEY_FILE="${KEYS_DIR}/system-WLKey.secure"
WL_CONFIG_FILE="${DOMAIN_DIR}/system-WLConfig.secure"
WLS_ENV="@WLS_ENV@"

Those are the last two headers from the last scripts.
# 20  
Old 11-02-2008
Again discrepancies in your requirements.

Code:
#!/bin/bash

cd /path/to/files/

read -p "MAN_SERVER_NAME: "   v1
read -p "BEA_INSTALL_DIR: "   v2
read -p "BASE_DOMAIN_NAME: "  v3
read -p "DOMAIN_NAME: "       v4
read -p "UNIX_SERVER: "       v5
read -p "USER HOME: "         v6
read -p "KEYS_SUB_DIR: "      v7
read -p "MAN_SERVER_PORT: "   v8


path="/path/to/tmp/dir/"

for file in *WLS

 do

   awk -F'@|}|\\${'  '
           		   /SERVER_NAME="@MAN_SERVER_NAME@"/   { $0=$1 v1 $3 }
          		   /LISTEN_PORT="@MAN_SERVER_PORT@"/   { $0=$1 v8 $3 }
           	           /BASE_DIR="@BEA_INSTALL_DIR@"/      { $0=$1 v2 $3 }
            	           /UNIX_SERVER_NAME="@UNIX_SERVER@"/  { $0=$1 v5 $3 }
	                   /DOMAIN_BASE=/ && /\/user_projects\// { p=v2 $3 v3; $0=$1 v2 $3 v3 $5 }
           		   /DOMAIN_DIR="/ && /\/@DOMAIN_NAME@"/  { $0=$1 p $3 v4 $5 }
            	           /MANSERVER_DIR=/ && /\/managedservers"/ { $0=$1 p $3 }
	             	   /KEYS_DIR="@USER_HOME@\/keys\//       { $0=$1 v6 $3 v3 $5 v7 $7 }

                       1'  v1="$v1" v2="$v2" v3="$v3" v4="$v4" v5="$v5" v6="$v6" v7="$v7" v8="$v8" "$file" > "$path"/"$file"
 done

# 21  
Old 11-20-2008
This may help you probably

#!/usr/bin/ksh

ls -l |grep .txt|nawk '{print $9}' > list

while read FILE

do

perl -pi -e 's/09\/01\/2009/09\/12\/2008/g' $FILE

done <list
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to replace stings in multiple text files

Good Evening Folks - Happy Friday! I have a need to replace a certain string in all .csv files from "0.00" to "#Missing" in my /app/hyp_app/files directory. Does anyone have a script they use regularly that's rather quick in performance? My files are rather large so I'm looking for a... (5 Replies)
Discussion started by: SIMMS7400
5 Replies

2. Shell Programming and Scripting

Getting error -: more tokens expected in shell script

Hi can someone help me to resolve the error for this condition if ] && ]; then i am passing the values $k and $kkm i am getting the error like "-: more tokens expected" Thanks in Advance (5 Replies)
Discussion started by: makigate
5 Replies

3. Shell Programming and Scripting

Shell script to find and replace contents of files in directory

Hi all This is my first post. Please bear with me with all my mistakes. I started learning shell since couple of days now and this might be quite basic for all, i want to search for files in a directory containing specific string and replace it with new string. The code i wrote is quite bulky... (2 Replies)
Discussion started by: theprogrammer
2 Replies

4. Shell Programming and Scripting

Script to find & replace a multiple lines string across multiple php files and subdirectories

Hey guys. I know pratically 0 about Linux, so could anyone please give me instructions on how to accomplish this ? The distro is RedHat 4.1.2 and i need to find and replace a multiple lines string in several php files across subdirectories. So lets say im at root/dir1/dir2/ , when i execute... (12 Replies)
Discussion started by: spfc_dmt
12 Replies

5. Shell Programming and Scripting

Need tokens in shell script

Hi All, Im writing a shell script in which I want to get the folder names in one folder to be used in for loop. I have used: packsName=$(cd ~/packs/Acquisitions; ls -l| awk '{print $9}') echo $packsName o/p: opt temp user1 user2 ie. Im getting the output as a string. But I want... (3 Replies)
Discussion started by: AB10
3 Replies

6. Shell Programming and Scripting

Strip log file with multiple tokens

I have app log files that need to be cleansed for readability, and my sed skills are not adequate. Each line has a long multi-segment header, which I am trying to remove, example below: : 2010.05.26 20:38:00.640--DatabaseSessionImpl(21447570)--Connection(26209441)--Thread(Thread)--SELECT... (4 Replies)
Discussion started by: migurus
4 Replies

7. Shell Programming and Scripting

Complex Search/Replace Multiple Files Script Needed

I have a rather complicated search and replace I need to do among several dozen files and over a hundred occurrences. My site is written in PHP and throughout the old code, you will find things like die("Operation Aborted due to....."); For my new design skins for the site, I need to get... (2 Replies)
Discussion started by: UCCCC
2 Replies

8. Shell Programming and Scripting

shell script to find and replace string in multiple files

I used the following script cd pathname for y in `ls *`; do sed "s/ABCD/DCBA/g" $y > temp; mv temp $y; done and it worked fine for finding and replacing strings with names etc. in all files of the given path. I'm trying to replace a string which consists of path (location of file) ... (11 Replies)
Discussion started by: pharos467
11 Replies

9. Shell Programming and Scripting

Shell script to parse/split input string and display the tokens

Hi, How do I parse/split lines (strings) read from a file and display the individual tokens in a shell script? Given that the length of individual lines is not constant and number of tokens in each line is also not constant. The input file could be as below: ... (3 Replies)
Discussion started by: yajaykumar
3 Replies

10. Shell Programming and Scripting

Find and Replace in multiple files (Shell script)

hi guys, Suppose you have 100 files in a folder and you want to replace all occurances of a word say "ABCD" in those files with "DCBA", how would you do it ??? jatin (13 Replies)
Discussion started by: jatins_s
13 Replies
Login or Register to Ask a Question