Web Hosting Talk







View Full Version : non-interactive file editing


innova
03-19-2003, 03:03 PM
I need some help using one of awk / sed / or something else.

ServerName = "Name"
SCPassword = "Password"

This needs to be replaced with:

ServerName = "Different Name"
SCPassword = "Newpass"

So I read up on sed a little, and tried this:

#!/bin/bash

NAME="Different Name"
sed "s/ServerName = "Name"/ServerName = "$NAME"/g" config.txt

Where config.txt is the name of the file I am modding.

This works ok, but the file does not stay changed!

Can someone point me in the right direction?

admin0
03-19-2003, 03:11 PM
Hi,

you forgot to put >> at the end.


sed "s/ServerName = "Name"/ServerName = "$NAME"/g" >> config.txt

> -- to overwrite the file
>> -- to append to the file.


:homer:

innova
03-19-2003, 03:32 PM
When I do that, it just sits there forever and doesnt do anything.

Do you mean:

config > config

OR

config >> config?

Neither give the desired result. Option 1 results in a 0-byte file, and option 2 results in duplicated entries.

admin0
03-19-2003, 03:41 PM
Hi,

why not have the script output the result, and you pass that to the file.

./script > filename

or,

have the script output to a temporary file, and replace the original file to the desired filename.

for example, you cannot

cat file1 | sed " " > file1

-- as whatever the output, the file will be of 0 byte.

however, you can

cat file1 | sed " " > file2
rm file1
mv file2 file1


:homer:

innova
03-19-2003, 03:46 PM
I was just testing that, thanks!

I am thinking this is a rather cumbersome way to solve a simple problem.

Maybe I should just use a skeleton config file as a php script, with my variables ready to replace:

ServerName = $servername

Then, when Servername value is inputted from order, I would just have that php file > config with values intact.

serial
03-19-2003, 03:50 PM
You can use vim/vi to easily find & replace all instances of something within a text file:

vim filename.txt <enter>
:%s,find,replace,g <enter>
:wq <enter>