The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
awk - replace number of string length from search and replace for a serialized array otrotipo Shell Programming and Scripting 1 07-10-2009 12:04 PM
Search, replace string in file1 with string from (lookup table) file2? gstuart Shell Programming and Scripting 9 06-08-2009 06:11 AM
Search for a string and replace the searched string in the same position in samefile ganesh_248 UNIX for Dummies Questions & Answers 27 03-23-2009 12:35 AM
sed - replace string question danmero Shell Programming and Scripting 4 01-04-2008 02:28 PM
How to replace all string instances found by find+grep umen Shell Programming and Scripting 0 12-06-2007 03:52 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 3 Weeks Ago
Mike AAA Mike AAA is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 2
sed/grep string replace question

Hi all,

I know this question has probably been answered before, but I am struggling with this problem, even after googling a million pages.

In a file named rdmt.conf
I need a single character replaced, the number in the line below

CUR_OC4J_ID=1

It will always appear after CUR_OC4J_ID in the file.

ie replaced by:
CUR_OC4J_ID=2

I need to be able to toggle this value a few times in my script.

I can find the character via a grep:
CUR_OC4J_ID=$(grep CUR_OC4J_ID $SCRIPTDIR/rdmt.conf | cut -d= -f2)

But I cant work out how to replace it.

I have even created some test files. I can use the sed command to replace "hello" with "no" in a file, but when it gets to CUR_OC4J_ID it just spits out text saying the number in which i wanted it replaced to, and then when you look at the file, its not changed. I thought I could make it easier (but not neater) and just find the current value, and replace the whole line because I can put a variable in the sed command......


Any help would be appreciated, I know this is easy, but I have googled forever.

A sed command that doesn't depend on what number is after CUR_OC4J_ID would be the best/neatest solution.
  #2 (permalink)  
Old 3 Weeks Ago
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
Is this what you're looking for?

Code:
awk -v var="2" 'BEGIN{FS=OFS="="}
/CUR_OC4J_ID/{$2=var}
{print}
' file > newfile
Regards
  #3 (permalink)  
Old 3 Weeks Ago
Scrutinizer Scrutinizer is online now
Registered User
  
 

Join Date: Nov 2008
Posts: 597
Alternatively you can also use sed. If your sed knows the -i option you can change the value in the file like this
Code:
newval=2
sed -i "s/\(CUR_OC4J_ID=\).*/\1$newval/" rdmt.conf
otherwise you'd have to use something like this:
Code:
sed "s/\(CUR_OC4J_ID=\).*/\1$newval/" rdmt.conf > rdmt.conf.new && mv -f rdmt.conf.new rdmt.conf
  #4 (permalink)  
Old 3 Weeks Ago
Mike AAA Mike AAA is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 2
Legends.

My sed didnt have the -i option but I used your second suggestion.

Thanks both of you for responding so quickly.
Reply

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 02:35 PM.


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