Reputation: 4058
I've got a property file where I want to do a property substitution, so I wrote a sed patter to change
host = 1234
with another value, but when I execute
echo "host = 1234" | sed 's/\#*\(host[ \t]*\)\=\([ \t]\d*\)/\1\=\1/g'
I got that the substitution is done (host =host) but the \2 atom is also appended to the end of the string (1234). How can I remove it?
`host =host 1234
Upvotes: 0
Views: 101
Reputation: 26086
The first problem is that \d
doesn't do what you think. Use [0-9]
at least.
You still get host =host
out, which seems crazy to me.
EDIT:
Okay
echo "host = 1234" | sed 's/#*\host[ \t]*=[ \t]*\([0-9]*\)/host = asdf/g'
I hope you get the idea.
But here's what you probably want:
sed '/^#/!s/[ \t]*\([^ \t]*\)[ \t]*=[ \t]*\([^ \t]*\)/\1 = newvalue/g' input_file
This will change anything = anything
to anything = newvalue
in non-commented lines of input_file
. To make it a specific key which is replaced by newvalue
, use:
sed '/^#/!s/[ \t]*\(host\)[ \t]*=[ \t]*\([^ \t]*\)/\1 = newvalue/g' input_file
to e.g. replace only lines reading host = anything
.
Upvotes: 2
Reputation: 28036
Does this suit your needs?
echo "host = 1234" | cut -d"=" -f 1
yields
host
Then,
echo "host = 1234" | cut -d"=" -f 1
yields
1234
Upvotes: 0