The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how can search a String in one text file and replace the whole line in another file kkraja UNIX for Dummies Questions & Answers 6 08-06-2008 08:23 AM
How to replace the string in unix file? param_it UNIX for Dummies Questions & Answers 5 06-26-2007 10:06 AM
replace a string with content from another file afatguy Shell Programming and Scripting 4 09-14-2006 10:25 PM
replace a string in a file ratan2204 Shell Programming and Scripting 7 05-10-2006 10:40 AM
replace string in binary file sg1207 Shell Programming and Scripting 2 07-29-2004 12:26 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-08-2008
tret tret is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 29
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 (permalink)  
Old 12-08-2008
tret tret is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 29
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 (permalink)  
Old 12-08-2008
tret tret is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 29
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 (permalink)  
Old 12-08-2008
Ikon's Avatar
Ikon Ikon is offline Forum Advisor  
Registered User
  
 

Join Date: Jul 2008
Location: Phoenix, Arizona
Posts: 669

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

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

  #5 (permalink)  
Old 12-08-2008
tret tret is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 29
Quote:
Originally Posted by Ikon View Post
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 (permalink)  
Old 12-08-2008
squrcles squrcles is offline
Registered User
  
 

Join Date: Nov 2008
Location: london/suburbia
Posts: 10
perl -p -i -e 's/original text string/replacementstring/g' file
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:13 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0