Global replace with perl in bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Global replace with perl in bash
# 1  
Old 05-13-2013
Global replace with perl in bash

I am newbie to perl,
I am trying to use the below syntax to replace globally a string with a variable.


Code:
$ bash -version
GNU bash, version 2.05b.0(1)-release (powerpc-ibm-aix5.1)
Copyright (C) 2002 Free Software Foundation, Inc.

$ perl -version
This is perl, v5.8.8 built for aix-thread-multi

Code:
# !/bin/bash
set -x
REF_URL="http://vmdis01.corp.testcompany.web/ABC_1.0_Version/index.htm"
cp test_url_main.txt test_url.txt
perl -pi -e "s/XXREF_URLXX/\Q${REF_URL}\E/g;" test_url.txt
cat test_url.txt

I am getting the following error

Code:
++ REF_URL=http://vmdis01.corp.testcompany.web/ABC_1.0_Version/index.htm
++ cp test_url_main.txt test_url.txt
++ perl -pi -e 's/XXREF_URLXX/\Qhttp://vmdis01.corp.testcompany.web/ABC_1.0_Version/index.htm\E/g;' test_url.txt
Bareword found where operator expected at -e line 1, near "0_Version"
        (Missing operator before Version?)
Backslash found where operator expected at -e line 1, near "htm\"
syntax error at -e line 1, near "0_Version"
Execution of -e aborted due to compilation errors.
++ cat test_url.txt
XXREF_URLXX

Any help is appreciated.
# 2  
Old 05-13-2013
This has to do with your choice of delimeter (/), since the string would expand to:
Code:
perl -pi -e "s/XXREF_URLXX/\Qhttp://vmdis01.corp.testcompany.web/ABC_1.0_Version/index.htm\E/g;" test_url.txt

...not a valid substitution command.

Try using a different one.

Code:
perl -pi -e "s|XXREF_URLXX|\Q${REF_URL}\E|g;" test_url.txt

This User Gave Thanks to Scott For This Post:
# 3  
Old 05-13-2013
That worked like charm, and thanks for the speedy reply !!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Global replace command help

Hello - I am trying to use a global replace command but its not working. Here is the cmd I am using in vi: :%s/OLD/NEW/g However, in my "NEW" I already have a "/" which is not making the replace work: :%s/mytestscript.com:33232/mytestscript:70245/test.com/g the... (2 Replies)
Discussion started by: DallasT
2 Replies

2. Programming

Global variable in for loop (BASH)

Hello, I'm trying to read the variable "pause" from a for loop without luck. The function is dependant on the outcome of the test within the loop. If i run this, pause is always 0 within the function. Any ideas? Thanks. pause=0 users=1 (for (( ; ; )) do speed=`cat speed.log` ... (7 Replies)
Discussion started by: shadyuk
7 Replies

3. Shell Programming and Scripting

Global exit listener in bash

I have a script with a whole lot of different functions and want to set teh script so that at any point a key or series of keys can be pressed to exit out and into the main menu function. Rather this than endlessly creating, 'Return to main' menus. Something along the lines of Ctrl+q for example... (1 Reply)
Discussion started by: 3therk1ll
1 Replies

4. UNIX for Dummies Questions & Answers

Vi global replace command

I am wondering what way, I can remove a certain text with nothing. for example: MyVariable=Y7UHNI to only: Y7UHNI removing 'MyVariable=' globally? thanks (1 Reply)
Discussion started by: DallasT
1 Replies

5. Shell Programming and Scripting

Global replace in vi with no escape required

Hi all, I want to replace a string of words but i dont want to worry about escaping the characters in that string . For e.g. If my string is "work/" and i want to replace it with "nowork" .I have to type %s/work\//nowork/g Is there a way by which I wont need to worry about the data in... (1 Reply)
Discussion started by: sachinpawar2308
1 Replies

6. Shell Programming and Scripting

Global search and replace across multiple files

Hi all I'm in need of a command which can replace a specified string with another string - across multiple files within multiple sub-directories (I intend to run it from / ) I've used the following to get a list of the files: find . | xargs grep <string1> But that's as far as I've got.... (7 Replies)
Discussion started by: huskie69
7 Replies

7. Shell Programming and Scripting

Global replace with sed

Hi, I need to change some strings from A to B in a number of files within a directory. Please can someone advise how do I do that? Many thanks (5 Replies)
Discussion started by: canary
5 Replies

8. Shell Programming and Scripting

Perl: Global Search and replace

I have a file where the rows correspond to individuals and the columns are about 106 variables. Each variable is coded as either ACGT, and "missing" is coded as blank. This is a tab delimited file. I'm trying to replace all blanks (" ") with 0. The simple script I have is only replacing some of the... (3 Replies)
Discussion started by: epi8
3 Replies

9. Shell Programming and Scripting

global replace...

Hi, I have a dir containing many shell scripts. Each of these shell scripts state a database name (always the same), which I need to change to be a new database. grep <database name> * brings back all the lines containing the database name. However, I need to change them to be something... (1 Reply)
Discussion started by: topcat8
1 Replies

10. UNIX for Dummies Questions & Answers

Global search ok...but replace?

Gurus, I have in my /tmp directory 26 files "filea", "fileb"..."filez". Each file contains the name of a database 'dwora' at many, many places within each file. My boss decided to change the name of the db so I need to do (what i'd call) a global search&replace of that string in all my... (0 Replies)
Discussion started by: alan
0 Replies
Login or Register to Ask a Question