find and replace text with a variable?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting find and replace text with a variable?
# 1  
Old 08-29-2005
Network find and replace text with a variable?

Is there a way that I can make the following work with using variables?

perl -pi -e 's#blah#hrm#ig' replacetext

but like this

var=blah
perl -pi -e 's#$var#hrm#ig' replacetext
# 2  
Old 08-29-2005
Quote:
Originally Posted by doublejz
Is there a way that I can make the following work with using variables?

perl -pi -e 's#blah#hrm#ig' replacetext

but like this

var=blah
perl -pi -e 's#$var#hrm#ig' replacetext
yes, there is - by using double quotes.
# 3  
Old 08-29-2005
Quote:
Originally Posted by vgersh99
yes, there is - by using double quotes.
hehe oh yea, maybe I should give up scripting while I am ahead. Smilie
# 4  
Old 08-31-2005
just replace ' to " is okay.
Code:
var=blah
var2=AAA
perl -pi -e "s#$var#$var2#ig" replacetext

cheers.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Replace variable string with text

Hi All, Hoping someone can help.... I am trying to work out how I can ammend a log file to remove variable strings in order to remove confidential information which I cant pass on. As an example I have used phone numbers. A large log file contains multiple lines containing something like the... (6 Replies)
Discussion started by: mutley2202
6 Replies

2. Shell Programming and Scripting

find and replace with variable -sed

Hi, I have a ksh script where I am trying to mask the password in the log files. $loc - is my directory $PGUIDE_DB_USER_PSW - is a variable that holds the password I am looking for find $loc/logs -type f -exec sed -i "s/$PGUIDE_DB_USER_PSW/*****/"g {} \; I get an error: ... (2 Replies)
Discussion started by: amitlib
2 Replies

3. Shell Programming and Scripting

Find a variable in a file and replace its value

HI , I can't find a solution to the following: In a simple menu script I want to capture the input from the user with "read" and use it as a value in a variable Rempages="some_value" which is in a different script file. So I have to perform a search and replace for this variable and only... (4 Replies)
Discussion started by: svetoslav_sj
4 Replies

4. Shell Programming and Scripting

Find and add/replace text in text files

Hi. I would like to have experts help on below action. I have text files in which page nubmers exists in form like PAGE : 1 PAGE : 2 PAGE : 3 and so on there is other text too. I would like to know is it possible to check the last occurance of Page... (6 Replies)
Discussion started by: lodhi1978
6 Replies

5. Shell Programming and Scripting

replace text in file using variable

Hi, I'm making a script that automaticaly set file size and path in xml file. I tried with : sed -i 's/BOOTPATH/TEST/g' file.xml it works fine but if I use a viriable : sed -i 's/BOOTPATH/$bootpathf/g' file.xml with this one, no change are made. I don't understand why. If a make a ... (13 Replies)
Discussion started by: Toug
13 Replies

6. UNIX for Dummies Questions & Answers

sed to replace text with a variable

I'm trying to replace text in a file with text from a variable I have the following in my script, but its not working: #!/bin/ksh echo "Enter the path to load scripts" read x echo "updating the templates" sed "s/CHANGE_ME_TO_LOAD_PATH/"$x"/g" LoadFiles.sh > LoadFiles2.sh I thought... (1 Reply)
Discussion started by: orahi001
1 Replies

7. Shell Programming and Scripting

find and replace variable

Is there an easy way of doing this cat file1 jkasjhjgfg LTRIM(RTRIM(aa_bb_cde)) aragsfdg LTRIM(RTRIM(aa_bb_cde)) aregfafdgfg sdgsfdagdfg gadfg eafgsadgsa asdgsfdgag LTRIM(RTRIM(aa_bb_cde)) rfghsdfhd I want to replace each occurence of LTRIM(RTRIM($x)) with LENGTH(LTRIM(RTRIM($x)))=0... (4 Replies)
Discussion started by: alfredo123
4 Replies

8. UNIX for Dummies Questions & Answers

Find and replace text PLEASE HELP

Dear friends please help, I have a large data file with few hundred lines. A small example is shown below: datafile is a file with few hundred lines with the third column has many different character stings: test 100 abc test 134 bcd test 356 cdf test 831 dfg test 720 fgh I need to... (6 Replies)
Discussion started by: bobo
6 Replies

9. UNIX for Dummies Questions & Answers

Find and replace text

test 100 abc test 134 bcd test 356 cdf test 831 dfg test 720 fgh Please advise how can I replace the abc, bcd....with ABC, BCD.... (1 Reply)
Discussion started by: bobo
1 Replies

10. Shell Programming and Scripting

Find and replace the value in a variable

I have a variable whose value is I="user1:x:1100:1200:ID for user1:/home/user1:/bin/ssh-dummy-shell" I want to replace the last part '/bin/ssh-dummy-shell' with '/bin/true' I tried using sed but it garbled sed 's/\/bin\/ssh-dummy-shell/\/bin\/true' $I Thanks for the help (5 Replies)
Discussion started by: aajmani
5 Replies
Login or Register to Ask a Question