How's your sed-fu?

milesleacy
Valued Contributor

I have a configuration file for an application that contains two lines of
text:

SERVER = server.domain.ext
CLIENT_NAME = client.domain.ext

I'd like to be able to change these lines with a simple script. I think sed
can get me there, but I'm having a little trouble getting it just right.

I've figured out how to replace "server.domain.ext" with "somethingelse" if
I know what the server.domain.ext string is. I don't want to rely on knowing
what the existing string is. What I want to do is replace everything after
"SERVER = " with a string of my choosing.

Any of the fine minds on these boards have and idea?

Thanks in advance,
Miles

1 REPLY 1

Not applicable

Hi Miles-

I've come to use awk more. It has a bit of a learning curve, but seems quite a bit more powerful.

Here's what I would use:

#!/bin/bash line1=`grep SERVER test.txt | awk -F" = " '{print $1 " = newvalue"}'` #returns SERVER = newvalue line2=`grep CLIENT_NAME test.txt | awk -F" = " '{print $1 " = newname"}` #returns CLIENT_NAME = newname # Then overwrite the file echo "$line1" > test.txt echo "$line2" >> test.txt exit 0

NOTE: Make sure you have the block quotes (``) around the grep/awk commands so the variable gets what is returned from the commands, not the string.

The grep command will pull out just that line, then awk will use " = " as a delimiter (with the F command) and then print $1 (first value in delimited list, SERVER) and the the string in quotes.

Then you just use echo and the greater thans to overwrite and append to the file.

Hope this helps

Ryan Harter
UW - Stevens Point
Workstation Developer
715.346.2716
Ryan.Harter at uwsp.edu

On Oct 31, 2008, at 11:03 AM, Miles Leacy wrote:

I have a configuration file for an application that contains two lines of text: SERVER = server.domain.ext CLIENT_NAME = client.domain.ext I'd like to be able to change these lines with a simple script. I think sed can get me there, but I'm having a little trouble getting it just right. I've figured out how to replace "server.domain.ext" with "somethingelse" if I know what the server.domain.ext string is. I don't want to rely on knowing what the existing string is. What I want to do is replace everything after "SERVER = " with a string of my choosing. Any of the fine minds on these boards have and idea? Thanks in advance, Miles <ATT00001.txt>