Visit Our UNIX and Linux User Community


sed/grep string replace question


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers sed/grep string replace question
# 1  
Old 10-31-2009
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  
Old 10-31-2009
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  
Old 10-31-2009
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  
Old 10-31-2009
Legends.

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

Thanks both of you for responding so quickly.
 

Previous Thread | Next Thread
Test Your Knowledge in Computers #262
Difficulty: Easy
During the Second World War, Alan Turing worked for the Government Code and Cypher School (GC&CS) at Bletchley Park, Britain's codebreaking centre that produced Ultra intelligence.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace string of a file with a string of another file for matches using grep,sed,awk

I have a file comp.pkglist which mention package version and release . In 'version change' and 'release change' line there are two versions 'old' and 'new' Version Change: --> Release Change: --> cat comp.pkglist Package list: nss-util-devel-3.28.4-1.el6_9.x86_64 Version Change: 3.28.4 -->... (1 Reply)
Discussion started by: Paras Pandey
1 Replies

2. Shell Programming and Scripting

Replace string in XML file with awk/sed with string from another

Sorry for the long/weird title but I'm stuck on a problem I have. I have this XML file: </member> <member> <name>TransactionID</name> <value><string>123456789123456</string></value> </member> <member> <name>Number</name> ... (9 Replies)
Discussion started by: cozzin
9 Replies

3. Shell Programming and Scripting

Help with Passing the Output of grep to sed command - to find and replace a string in a file.

I have a file example.txt as follows :SomeTextGoesHere $$TODAY_DT=20140818 $$TODAY_DT=20140818 $$TODAY_DT=20140818I need to automatically update the date (20140818) in the above file, by getting the new date as argument, using a shell script. (It would even be better if I could pass... (5 Replies)
Discussion started by: SriRamKrish
5 Replies

4. Shell Programming and Scripting

Grep and sed (replace string in patterned lines)

Grep and Sed (replace string in patterned lines) Hi all, I want to grep for "PATTERN" and only if "PATTERN" is in a line, this line shall be used as replacement input e.g. for SED. I don't get it running in one line. NOT RUNNING - just first idea... I don't know how to redirect grep... (2 Replies)
Discussion started by: unknown7
2 Replies

5. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

6. Shell Programming and Scripting

QUESTION1: grep only exact string. QUESTION2: find and replace only exact value with sed

QUESTION1: How do you grep only an exact string. I am using Solaris10 and do not have any GNU products installed. Contents of car.txt CAR1_KEY0 CAR1_KEY1 CAR2_KEY0 CAR2_KEY1 CAR1_KEY10 CURRENT COMMAND LINE: WHERE VARIABLE CAR_NUMBER=1 AND KEY_NUMBER=1 grep... (1 Reply)
Discussion started by: thibodc
1 Replies

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

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

9. Shell Programming and Scripting

sed: replace string with another string (with spaces)

Hi I have an XML file with strings XABCD, XEFGHX and XIJKLX. I would like to replace XABCDX with "This is the first string", XEFGHX with "This is the second string" and XIJKLX with "This is the third string". What is the best way to implement this? Should I have a file with the data that is... (4 Replies)
Discussion started by: zmfcat1
4 Replies

10. Shell Programming and Scripting

sed - replace string question

Hi Everybody Uning sed I need to replace: "count(user) = 0" -o "uid = ftp" -n -o "gid = ftp" by "read only = no" Thanks in advance. (4 Replies)
Discussion started by: danmero
4 Replies

Featured Tech Videos