Help to change value from a file.

How can I change the value of "INI_TEST" from "YES" to "NO" in the setup.ini file by command?

[galaxy@test01 test]$ cat setup.ini
INI_START YES
INI_END YES
INI_TEST YES

Thank u in advance!

If it just one occurrence, then manual changing to NO will help.

Else,

sed -e 's/\(INI_TEST\) YES/\1 NO/g' setup.ini > setup.ini.new

Of if you have a -i option for your sed,

sed -i -e 's/\(INI_TEST\) YES/\1 NO/g' setup.ini 
sed 's/INI_TEST YES/INI_TEST NO/g' text > text
#substitute INI_TEST YES with INI_TEST NO and overwrite the original file

the above command will fail. it seems that the original file will become empty. Can someone explain?

Redirect the output to some other file and then rename it later. Else the read-write operations are being carried out on a single file.
Like I mentioned before, if you have a -i option for your sed, use that for operations like read and write on a single file.

Thanks vino!

It seems that I can't change the value straightly ......
Is there any method to change the value straightly? ( not use sed )

Why cant you change the value manually ? What is it that you are trying and what is the error that you are facing ? Check the permissions et al..

I think it should be as simple as opening the file in your favourite editor and changing the value.

I want to make some scripts to change the System configuration automatically. My server is Redhat Enterprise 3.0, it seems I can't use -i option. But thank u in advance. :wink:

Did you try the sed command that was posted earler ?

sed -e 's/\(INI_TEST\) YES/\1 NO/g' setup.ini > setup.ini.new

YES, it works. I want to confirm if there is any much easier way to do that or not.

$ cat setup.ini
INI_START YES
INI_END YES
INI_TEST YES

$ awk '$1=="INI_TEST"{$2="NO"}{a[NR]=$0}END{close(FILENAME);for(i=1;i<=NR;i++)print a>FILENAME}' setup.ini

$ cat setup.ini
INI_START YES
INI_END YES
INI_TEST NO

It does work, perfect!

Thank u all in advance!

There must be a blue moon tonight! This actually looks more readable in perl!

$ cat setup.ini
INI_START YES
INI_END YES
INI_TEST YES
$ perl -pi -e 'if ($_ =~ /^INI_START/) {s/YES/NO/}' setup.ini
$ cat setup.ini
INI_START NO
INI_END YES
INI_TEST YES
$

Thank u Perderabo for giving me another way to do it. :wink:

One more way of doing it man, using gsub in awk

where f2.txt is your input file
Gaurav

awk '{sub("INI_TEST YES","INI_TEST NO"); print}' f2.txt
ruby -i -pe 'sub(/(INI_TEST )YES/,"\\1NO")' file.ini