You are not logged in.

#1 2014-02-10 10:54:45

z1lt0id
Member
Registered: 2012-09-20
Posts: 177

Replacing a text with a variable using SED and bash

I have a file called prog.conf with a range of variables in it and I am trying to replace a variable in the conf file.

The conf file is called using the SOURCE command

I want it to incrementaly update the variable.

So if it is 1 one it becomes 2, etc.

I've tried the following but SED doesn't like it and it errors out.  Here is the code.

In the prog.conf file there is a line that says
input_variable=1

I've tried



prev_seg="input_variable=$input_variable"
read wait
input_variable=$(($input_variable + 1))
next_seg="input_variable=$input_variable"

sed -i "$prev_seg/$next_seg/g" prog.conf

There is a probably much better way of doing all of this, but SED is my main issue.  Any ideas?

Offline

#2 2014-02-10 12:03:21

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,332
Website

Re: Replacing a text with a variable using SED and bash

awk -F '=' '/^input_variable/ { print "input_variable=" $2+1; } !/^input_variable/ {print $0;}' infile > outfile
mv outfile infile

EDIT: actually sed will do this just fine too if you use double quotes around the sed string - but yours fails as that is just not a sed command.  That should be:

sed -i "s/$prev_seg/$next_seg/" prog.conf

You need to use the 's' command, and the 'g' is irrelevant in this case.

Last edited by Trilby (2014-02-10 13:02:16)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

Board footer

Powered by FluxBB