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
# 8  
Old 10-27-2008
Assuming the same format and your initial conditions, try and adapt the following:

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

cd /full/path/to/your/ABC_scripts

read -p "enter 1st value: "  v1
read -p "enter 2rd value: "  v2
read -p "enter 3rd value: "  v3
# ... Enter more variables here ( v4, v5,...), in the same way as above.

read -p "enter full path to temp dir: "  path

#If you want the path hardcoded use,
# path="/full/path/to/temp-dir"



for file in *ABC

 do

  awk -F= '/@MAN_NAME@/{$0=$1"="v1}
           /@PORT_NUM@/{$0=$1"="v2}
           /@TEST_NUM@/{$0=$1"="v3}

           #/@other_tags_here@/{ "preserve same format as above" }

                                  1' v1="$v1" v2="$v2" v3="$v3" "$file" > "$path"/"$file" 

 done


Last edited by rubin; 10-27-2008 at 09:44 PM.. Reason: chmod 755 not needed
# 9  
Old 10-28-2008
deleted

Last edited by cbo0485; 10-28-2008 at 11:34 AM..
# 10  
Old 10-28-2008
Okay, forget that last part, it was b/c I was trying to read the files in the same directory I was trying to then write them to.

One other issue though, the scripts that have the tags in them that I'm trying to replace have these lines:

Code:
S_N="@A_S_N@"
B_D="@B_I_D@"
D_B="${B_D}/user_projects/@B_D_N@"
D_D="${D_B}/@D_N@"
S_U="t3://@U_S@:@A_S_P@"
A_U="t3://@A_U_S@:@A_S_P@"
K_D="@U_H@/keys/@B_D_N@/@K_S_D@"

So I would be expecting the tags to get replaced with the values I entered, but instead I'm like in the A_U line, instead of getting:

A_U="t3://U_S_Tag_Value:A_S_P_Tag_Value"

I get:

A_U=A_S_P_Tag_Value

No quotations, the colon was taken out, and the first tag value was completely taken out.

Same with the rest of them, all the quotations are gone.



This is my code, tag names are changed and stuff like that, but this is the exact structure of it:

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

cd /path/to/ABC/scripts/

read -p "TAG_1: "  v1
read -p "TAG_2: "  v2
read -p "TAG_3: "  v3
read -p "TAG_4: "  v4
read -p "TAG_5: "  v5
read -p "TAG_6: "  v6
read -p "TAG_7: "  v7
read -p "TAG_8: "  v8
read -p "TAG_9: "  v9
read -p "TAG_10: "  v10
read -p "TAG_11: "  v11
read -p "TAG_12: "  v12
read -p "TAG_13: "  v13

# ... Enter more variables here ( v4, v5,...), in the same way as above.

#read -p "/path/to/tmp/: "  path

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



for file in *ABC

 do

  awk -F= '/@TAG_1@/{$0=$1"="v1}
           /@TAG_2@/{$0=$1"="v2}
           /@TAG_3@/{$0=$1"="v3}
           /@TAG_4@/{$0=$1"="v4}
           /@TAG_5@/{$0=$1"="v5}
           /@TAG_6@/{$0=$1"="v6}
           /@TAG_7@/{$0=$1"="v7}
           /@TAG_8@/{$0=$1"="v8}
           /@TAG_9@/{$0=$1"="v9}
           /@TAG_10@/{$0=$1"="v10}
           /@TAG_11@/{$0=$1"="v11}
           /@TAG_12@/{$0=$1"="v12}
           /@TAG_13@/{$0=$1"="v13}



#/@other_tags_here@/{ "preserve same format as above" }

                                  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


Last edited by cbo0485; 10-28-2008 at 11:46 AM..
# 11  
Old 10-28-2008
Quote:
Originally Posted by cbo0485
Code:
S_N="@A_S_N@"
B_D="@B_I_D@"
D_B="${B_D}/user_projects/@B_D_N@"
D_D="${D_B}/@D_N@"
S_U="t3://@U_S@:@A_S_P@"
A_U="t3://@A_U_S@:@A_S_P@"
K_D="@U_H@/keys/@B_D_N@/@K_S_D@"

....
Well, the situation has changed, your tokens have special characters in them, and you need to escape all of them. See the sample below and make the proper changes to your code.

Code:
awk -F= '/\$\{B_D\}\/user_projects\/@B_D_N@/{$0=$1"="v1}
          /t3:\/\/@U_S@:@A_S_P@/            {$0=$1"="v2}
          /@U_H@\/keys\/@B_D_N@\/@K_S_D@/   {$0=$1"="v3}

          ## repeat the same thing for all of your tokens, using their actual names, and escape special chars:     - $ / + * ? . etc 

                                  1' v1="$v1" v2="$v2" v3="$v3" "$file" > "$path"/"$file"

Quote:
awk -F= '/@TAG_1@/ <---- ( In case you missed it ) This must be ----> /@A_S_N@/ {$0=$1"="v1}

# same thing for the other lines, put actual token names !
....
Quote:
This is my code, tag names are changed and stuff like that, but this is the exact structure of it:
...
That's the point, the structure looks fine, but it's the names of the tokens that are important, the script depends totally on their unique names and formation.

The code worked fine for me, look at a test using the above adjustments, with three random tokens given above:

Code:
# ./replace.sh

enter 1st value:
"t3://U_S_Tag_Value:A_S_P_Tag_Value"
enter 2nd value:
"other variable goes here"
enter 3rd value:
"000 000 000"
enter full path to temp dir:
/home/user/test

------

$ ll test
-rw-r--r--   1 user       sys            557 Oct 28 19:37 file_1ABC
-rw-r--r--   1 user       sys            557 Oct 28 19:37 file_2ABC
-rw-r--r--   1 user       sys            557 Oct 28 19:37 file_3ABC
-rw-r--r--   1 user       sys            557 Oct 28 19:37 file_4ABC


$ cat test/file_2ABC

##################################
#Header of script, place values in variable names#
##################################
S_N="@A_S_N@"
B_D="@B_I_D@"
D_B="t3://u_s_Tag_Value:a_s_p_Tag_Value"
D_D="${D_B}/@D_N@"
S_U="other variable goes here"
A_U="t3://@A_U_S@:@A_S_P@"
K_D="000 000 000"
VAR_MAN_NAME="@MAN_NAME@"
VAR_PORT_NUM="@PORT_NUM@"
VAR_TEST_NUM="@TEST_NUM@"
###################################
#---------------Script Logic---------------------#
###################################
.
.
.
This section uses all the variable names, and doesn't use any flags


Last edited by rubin; 10-29-2008 at 12:19 AM..
# 12  
Old 10-29-2008
Quote:
Originally Posted by rubin
Well, the situation has changed, your tokens have special characters in them, and you need to escape all of them. See the sample below and make the proper changes to your code.

Code:
awk -F= '/\$\{B_D\}\/user_projects\/@B_D_N@/{$0=$1"="v1}
          /t3:\/\/@U_S@:@A_S_P@/            {$0=$1"="v2}
          /@U_H@\/keys\/@B_D_N@\/@K_S_D@/   {$0=$1"="v3}

          ## repeat the same thing for all of your tokens, using their actual names, and escape special chars:     - $ / + * ? . etc 

                                  1' v1="$v1" v2="$v2" v3="$v3" "$file" > "$path"/"$file"





That's the point, the structure looks fine, but it's the names of the tokens that are important, the script depends totally on their unique names and formation.

The code worked fine for me, look at a test using the above adjustments, with three random tokens given above:

Code:
# ./replace.sh

enter 1st value:
"t3://U_S_Tag_Value:A_S_P_Tag_Value"
enter 2nd value:
"other variable goes here"
enter 3rd value:
"000 000 000"
enter full path to temp dir:
/home/user/test

------

$ ll test
-rw-r--r--   1 user       sys            557 Oct 28 19:37 file_1ABC
-rw-r--r--   1 user       sys            557 Oct 28 19:37 file_2ABC
-rw-r--r--   1 user       sys            557 Oct 28 19:37 file_3ABC
-rw-r--r--   1 user       sys            557 Oct 28 19:37 file_4ABC


$ cat test/file_2ABC

##################################
#Header of script, place values in variable names#
##################################
S_N="@A_S_N@"
B_D="@B_I_D@"
D_B="t3://u_s_Tag_Value:a_s_p_Tag_Value"
D_D="${D_B}/@D_N@"
S_U="other variable goes here"
A_U="t3://@A_U_S@:@A_S_P@"
K_D="000 000 000"
VAR_MAN_NAME="@MAN_NAME@"
VAR_PORT_NUM="@PORT_NUM@"
VAR_TEST_NUM="@TEST_NUM@"
###################################
#---------------Script Logic---------------------#
###################################
.
.
.
This section uses all the variable names, and doesn't use any flags


Here is one of the header of one of the actual scripts for an example.

Code:
SERVER_NAME="@ADMIN_SERVER_NAME@"
BASE_DIR="@BEA_INSTALL_DIR@"
DOMAIN_BASE="${BASE_DIR}/user_projects/@BASE_DOMAIN_NAME@"
DOMAIN_DIR="${DOMAIN_BASE}/@DOMAIN_NAME@"
SERVER_URL="t3://@UNIX_SERVER@:@ADMIN_SERVER_PORT@"
ADMIN_URL="t3://@ADMIN_UNIX_SERVER@:@ADMIN_SERVER_PORT@"
KEYS_DIR="@USER_HOME@/keys/@BASE_DOMAIN_NAME@/@KEYS_SUB_DIR@"

Some of those aren't inside the tags and I'm only looking to replace the tags, not entire variable names. And as you can see some variables are made up of multiple tags b/c those shorter tags may be used in other parts of the script as well. But the variable is not needed, just part of it(the tag).

There are only two tags that will have something besides a number or letter, and that's the @BEA_INSTALL_DIR@ and the @USER_HOME@ directory.
# 13  
Old 10-29-2008
Can you post :

- All of your variables ( with all the tokens ) in your ABC files ,
- An exact sample of how the variables will look like after the replacement ?

Does the script work for you for variables with a single token in them ( only one set of tags @..@ ) ?
# 14  
Old 10-29-2008
Quote:
Originally Posted by rubin
Can you post :

- All of your variables ( with all the tokens ) in your ABC files ,
- An exact sample of how the variables will look like after the replacement ?

Does the script work for you for variables with a single token in them ( only one set of tags @..@ ) ?
It works, but for some reason it gets rid of the quotations that are around the tokens.
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

# ... Enter more variables here ( v4, v5,...), in the same way as above.

#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}
           /@BEA_INSTALL_DIR@/{$0=$1"="v2}
           /@BASE_DOMAIN_NAME@/{$0=$1"="v3}
           /@DOMAIN_NAME@/{$0=$1"="v4}
           /@ADMIN_SERVER_NAME@/{$0=$1"="v5}
           /@UNIX_SERVER@/{$0=$1"="v6}
           /@ADMIN_SERVER_PORT@/{$0=$1"="v7}
           /@ADMIN_UNIX_SERVER@/{$0=$1"="v8}
           /@USER_HOME@/{$0=$1"="v9}
           /@KEYS_SUB_DIR@/{$0=$1"="v10}
           /@MAN_SERVER_PORT@/{$0=$1"="v11}
           /@WLS_ENV@/{$0=$1"="v12}
           /@WEBLOGIC_VERSION@/{$0=$1"="v13}



#/@other_tags_here@/{ "preserve same format as above" }

                                  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



Header Sections

Code:
SERVER_NAME="@MAN_SERVER_NAME@"
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"

Code:
SERVER_NAME="@MAN_SERVER_NAME@"
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"

Code:
SERVER_NAME="@ADMIN_SERVER_NAME@"
BASE_DIR="@BEA_INSTALL_DIR@"
DOMAIN_BASE="${BASE_DIR}/user_projects/@BASE_DOMAIN_NAME@"
DOMAIN_DIR="${DOMAIN_BASE}/@DOMAIN_NAME@"

Code:
SERVER_NAME="@MAN_SERVER_NAME@"
BASE_DIR="@BEA_INSTALL_DIR@"
DOMAIN_BASE="${BASE_DIR}/user_projects/@BASE_DOMAIN_NAME@"
MANSERVER_DIR="${DOMAIN_BASE}/managedservers"

Code:
SERVER_NAME="@ADMIN_SERVER_NAME@"
BASE_DIR="@BEA_INSTALL_DIR@"
DOMAIN_BASE="${BASE_DIR}/user_projects/@BASE_DOMAIN_NAME@"
DOMAIN_DIR="${DOMAIN_BASE}/@DOMAIN_NAME@"
WLS_VERSION="@WEBLOGIC_VERSION@"

Code:
SERVER_NAME="@ADMIN_SERVER_NAME@"
BASE_DIR="@BEA_INSTALL_DIR@"
DOMAIN_BASE="${BASE_DIR}/user_projects/@BASE_DOMAIN_NAME@"
DOMAIN_DIR="${DOMAIN_BASE}/@DOMAIN_NAME@"
SERVER_URL="t3://@UNIX_SERVER@:@ADMIN_SERVER_PORT@"
ADMIN_URL="t3://@ADMIN_UNIX_SERVER@:@ADMIN_SERVER_PORT@"
KEYS_DIR="@USER_HOME@/keys/@BASE_DOMAIN_NAME@/@KEYS_SUB_DIR@"

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