sed - How to replace right part of equal sign (=) on a line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed - How to replace right part of equal sign (=) on a line
# 1  
Old 12-25-2013
sed - How to replace right part of equal sign (=) on a line

Hello.

Using a bash script , I have a variable name for the file I want to modify
Code:
FILE_TO_EDIT="/etc/my_config_file"

And I have a variable name for the parameter to change
Code:
PARAMETER="fallback_node"
PARAMETER_NEW_VALUE="http://my_server_name.com/new_path"

A config file may contain :

1°) just and only just :
Code:
# fallback_node = http://example.com/path/to/sync

or 2°) just and only just :
Code:
fallback_node = http://my_server_name.com/some_web

In case 1 just add
Code:
"$PARAMETER = $PARAMETER_NEW_VALUE"

just under the commented line

In case 2
a) just replace the right part of the equal sign with $PARAMETER_NEW_VALUE
or
b) comment out the ligne and add a new ligne with. "$PARAMETER = $PARAMETER_NEW_VALUE"

Any help is welcome
# 2  
Old 12-25-2013
You could try this. Make sure the variables are sed-safe though..

Code:
sed "s;^[# ]*\(${PARAMETER} * = *\)\(.*\);# \1\2\n\1${PARAMETER_NEW_VALUE};" "${FILE_TO_EDIT}"

# 3  
Old 12-26-2013
Quote:
Originally Posted by neutronscott
You could try this. Make sure the variables are sed-safe though..

Code:
sed "s;^[# ]*\(${PARAMETER} * = *\)\(.*\);# \1\2\n\1${PARAMETER_NEW_VALUE};" "${FILE_TO_EDIT}"

Should be great if you can explain the syntax for my learning.

Any way I will try this.

---------- Post updated at 19:57 ---------- Previous update was at 14:00 ----------

Quote:
Originally Posted by neutronscott
You could try this. Make sure the variables are sed-safe though..

Code:
sed "s;^[# ]*\(${PARAMETER} * = *\)\(.*\);# \1\2\n\1${PARAMETER_NEW_VALUE};" "${FILE_TO_EDIT}"

Small bug.

Parameters :
Code:
PARAMETER="fallback_node"
PARAMETER_NEW_VALUE="http://localhost/FF_DB"

Initial data in config file :

Code:
[nodes]
# You must set this to your client-visible server URL.
# fallback_node = http://example.com/path/to/sync

Data after running sed command :

Code:
[nodes]
# You must set this to your client-visible server URL.
# fallback_node = http://example.com/path/to/sync
fallback_node = http://localhost/FF_DB

Good.
Now editing the config file with some other data and we get :

Code:
[nodes]
# You must set this to your client-visible server URL.
# fallback_node = http://example.com/path/to/sync
fallback_node = http://localhost/OTHER_TEST

Data after running sed command :

Code:
[nodes]
# You must set this to your client-visible server URL.
# fallback_node = http://example.com/path/to/sync
fallback_node = http://localhost/FF_DB
# fallback_node = http://localhost/OTHER_TEST
fallback_node = http://localhost/FF_DB

Something like :
1°) Add new line with new parameter after the 1st line which is commeted out when token is encounted
2°) comment out line with actual parameter the 1st line which is not commeted out when token is encounted
3°)1°) Add new line with new parameter after the next line which is commeted out when token is encounted

Any help is welcome
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script - Replace just part of a single line in a file.....

Hey guy's.... I new here, But im working on a school project, and I am not really good at programming. In fact, this is the only programming class that I need because programming is not what I am majoring in. But I have everything done in this shell script except for this last part..... ... (9 Replies)
Discussion started by: hxdrummerxc
9 Replies

2. Shell Programming and Scripting

sed to replace the matching pattern with equal number of spaces

Hi I have written a shell script which used sed code below sed -i 's/'"$Pattern"'/ /g' $FileName I want to count the length of Pattern and replace it with equal number of spaces in the FileName. I have used $(#pattern) to get the length but could not understand how to replace... (8 Replies)
Discussion started by: rakeshkumar
8 Replies

3. Linux

Linux command to find and replace occurance of more than two equal sign with "==" from XML file.

Please help me, wasted hrs:wall:, to find this soulution:- I need a command that will work on file (xml) and replace multiple occurrence (more than 2 times) Examples 1. '===' 2. '====' 3. '=======' should be replaced by just '==' Note :- single character should be replaced. (=... (13 Replies)
Discussion started by: RedRocks!!
13 Replies

4. Shell Programming and Scripting

get all the strings after the equal sign

hello just want to ask how you do this? a="b=abc def ghi" c=<all the strings after b=> echo $c output will be abc def ghi thanks!! (1 Reply)
Discussion started by: h0ujun
1 Replies

5. Shell Programming and Scripting

SED - replace only on part of the string

Hello there, I need some help. I have a file containing this : $ cat file PARM1=(VAL11),PARM2=(VAL21,VAL22,VAL23),PARM3=(VAL31),PARM4=(VAL41,VAL42) and I need to replace all the ',' by '|' but only those which are between brackets. Output would be :... (10 Replies)
Discussion started by: Sephiburp
10 Replies

6. Shell Programming and Scripting

replace part of text of a line

Gurus, You know, I believe you do:-), the comnand uname -r give you the kernel version: serverA:~# uname -r 2.6.26-1-xen-amd64So, I want to replace this output inside in the line below that is inside the file: kernel = '/boot/vmlinuz-2.6.26-1-xen-amd64'Suppose, you move this file to ther... (2 Replies)
Discussion started by: iga3725
2 Replies

7. Shell Programming and Scripting

Sed replace characters not equal to an expression

Hi all, Suppose I have a file with the contents below, and I only want to print words %S_ then | sort -u. ------------------------------ The %S_MSG that starts with '%.*s' is too long. Maximum length is %d. The %S_MSG name '%.*s' contains more than the maximum number of prefixes. The... (5 Replies)
Discussion started by: poldo
5 Replies

8. Shell Programming and Scripting

Replace part of a line with sed/awk

Hello I have a document and in this document I have several occurrence of "VAR == xxxxxxx" and xxxxx can be anything. I don't know what it is. I want to replace the 'xxxxx's with something I know. What I know however, is the line numbers of the VAR =='s in the file. How can I replace... (1 Reply)
Discussion started by: alirezan
1 Replies

9. Shell Programming and Scripting

Replace Entire line if any part matches regexp

Hey guys, I have a file that I've slowly been awking, seding, and greping for data entry. I am down to pull the addresses out to insert them into an excel file. Each address is a few lines, but i want to put a semicolon delimiter in between each address so I can export the text file into excel and... (6 Replies)
Discussion started by: Cocoabean
6 Replies

10. Shell Programming and Scripting

using sed to replace a part of string

Hi, I have files that are named front1.txt to front999.txt. They are all in the same directory. To change "front" to "back", I am doing something like this. for file in *.txt; do new=`echo $file | sed 's/^**/back/g'` mv $file $new done My problem is what if files are named... (6 Replies)
Discussion started by: csejl
6 Replies
Login or Register to Ask a Question