Replace string in a file w/ a variable value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace string in a file w/ a variable value
# 1  
Old 12-08-2008
Replace string in a file w/ a variable value

I am trying to replace the default home page for several mac user accounts, I wrote a script that will hunt the files down and replace them with a pre-configured set. The problem I am having is that the download destination path for the browser is hard coded into a .plist (text config file) file along with the homepage. The issue is that if I replace this file with a pre-configured version the homepage is changed successfully but the download location is then wrong and the browser won't be able to download anything.

the download path originally is something like /Users/rob/Desktop
the altered one is something like /Users/sample/Desktop

Because the /Users/sample/Desktop doesn't match the user I am replacing files for, there is a problem.

So I would like to set the location in the pre-configured file to something like /Users/[PATTERN//PATTERN]/Desktop and have the script open the text file, look for the pattern and replace it with the value of a variable called $USER_NAME which I already have holding the proper user name earlier in the script.

How can I have the script look inside the .plist file, find the pattern and replace it with a variable value?

Thanks,
Rob
# 2  
Old 12-08-2008
Found this on another forum in case anyone is interested, seems straight forward enough.

Quote:
The 'sed' utility is what you want. It can search and replace text using regular expressions. You should be able to 'cat' the resin.conf file into sed, do the replacement, and redirect the output back to resin.conf, something like this:

Code:
cat resin.conf | sed -e 's/ABC/XYZ/' > resin.conf
where ABC is a regular expression matching what you want to find, and XYZ is what you want to replace it with. There's a sed micro-primer in the advanced bash guide that may help you figure out how to insert a variable name into a sed expression.
# 3  
Old 12-08-2008
Ok so the following works...... almost

Code:
cat sample.txt | sed -e 's/abc/xyz/' >> result.txt

The only problem is, I want to use variables instead of abc and xyz

so something like

Code:
SEARCH="REPLACETHISTEXT"
REPLACE="Hello"

cat sample.txt | sed -e 's/$SEARCH/$REPLACE/' >> result.txt

When I try this, nothing happens, are the variables not available due to the pipe? Is there a way around this?

Thanks,
Rob
# 4  
Old 12-08-2008
Code:
SEARCH="REPLACETHISTEXT"
REPLACE="Hello"

cat sample.txt | sed -e "s/$SEARCH/$REPLACE/" >> result.txt

# 5  
Old 12-08-2008
Quote:
Originally Posted by Ikon
Code:
SEARCH="REPLACETHISTEXT"
REPLACE="Hello"

cat sample.txt | sed -e "s/$SEARCH/$REPLACE/" >> result.txt

I actually JUST tried this as a guess and it worked. Can you tell me what difference there is between the single quotes and double quotes that fixes this?

Thanks,
Rob
# 6  
Old 12-08-2008
perl -p -i -e 's/original text string/replacementstring/g' file
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to replace string in variable?

Hello, I have simple while and for loops in a shell script and I would like to replace some characters in COL2 when I run it. I am on ubuntu 14.04 while read COL1 COL2 COL3 COL4 do name=$COL2 #cat $name | sed -i "s|_| |g" $name for i in $COL3 $COL4 do some codes ...... run $i b="$name"... (11 Replies)
Discussion started by: baris35
11 Replies

2. Shell Programming and Scripting

Replace substring from a string variable

Hi, Wish to remove "DR-" from the string variable (var). var="DR-SERVER1" var=`echo $var | sed -e 's/DR-//g'` echo "$var" Expected Output: However, I get the below error: Can you please suggest. (4 Replies)
Discussion started by: mohtashims
4 Replies

3. UNIX for Beginners Questions & Answers

Views How to replace a CRLF char from a variable length file in the middle of a string in UNIX?

My sample file is variable length, with out any field delimiters. It has min of 18 chars length and the 'CRLF' is potentially between 12-14 chars. How do I replace this with a space? I still want to keep end of record, but just want to remove these new lines chars in the middle of the data. ... (7 Replies)
Discussion started by: chandrath
7 Replies

4. 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

5. Shell Programming and Scripting

How to replace a string with a variable in a file using sed?

I have a file having some text like: PATH_ABC=/user/myLocation I have to replace "/user/myLocation" with a session variable say, $REPLACE_PATH, where $REPLACE_PATH=/user/myReplaceLocation The following sed command is not working. It is writing PATH_ABC=$REPLACE_PATH in the file ... (2 Replies)
Discussion started by: SKhan
2 Replies

6. Shell Programming and Scripting

replace (sed?) a string in file with multiple lines (string) from variable

Can someone tell me how I can do this? e.g: a=$(echo -e wert trewt ertert ertert ertert erttert erterte rterter tertertert ert) How do i replace the STRING with $a? I try this: sed -i 's/STRING/'"$a"'/g' filename.ext but this don' t work (2 Replies)
Discussion started by: jforce
2 Replies

7. Shell Programming and Scripting

Replace string in file with a variable value

Hi Fellows, I am new to shell, please help we me out in this.. i have file which some lines like this.. $$param1='12-jan-2011' $$param2='14-jan-2011' $$param3='30-jan-2011' . . .....so on.. I want to change $$param3 to '31-dec-2011'. i have variable which is storing(30-jan-2011 this... (1 Reply)
Discussion started by: victor369
1 Replies

8. Shell Programming and Scripting

Find and replace string from file which contains variable and path - SH

e.g. /home/$USER/.config replace it with "" (empty) Is this possible? I think you should play a bit with sharps ## and sed:b: (2 Replies)
Discussion started by: hakermania
2 Replies

9. AIX

Replace string with asterisk(*) in variable

I was trying to replace a string ( for eg - @@asterisk@@ to * ) in variable using cat $INFILE | while read LINE do stmt1=`echo $LINE | sed 's/@@asterisk@@/\*/g'` stmt=$stmt' '$stmt1 stmt2=`echo $LINE` STATEMENT=$STATEMENT' '$stmt2 done echo 'Statement with sed -- > '... (5 Replies)
Discussion started by: Vaddadi
5 Replies

10. Shell Programming and Scripting

Using sed to replace a string in file with a string in a variable that contains spaces

Hi, i call my shell like: my_shell "my project name" my script: #!/bin/bash -vx projectname=$1 sed s/'PROJECT_NAME ='/'PROJECT_NAME = '$projectname/ <test_config_doxy >temp cp temp test_config_doxy the following error occurres: sed s/'PROJECT_NAME ... (2 Replies)
Discussion started by: vivelafete
2 Replies
Login or Register to Ask a Question