Config file edition by script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Config file edition by script
# 1  
Old 10-25-2012
Config file edition by script

Hello.
I am looking for a simple way to modify config files after a first install.
I have a file containing the good key=value

good_config.txt
Code:
SSHAuthorizedKeys="authorized_keys"
SSHD_PORT=54321
ENABLE_PASSDB_AUTHENTICATION="0"
ENABLE_SSH_AUTHENTICATION="1"
ENABLE_SU_AUTHENTICATION="0"
ENABLE_USER_DB="0"
NX_LOG_LEVEL=5

Now the job :
Code:
#
# easy - copy config_file to tmp_file
# easy - remove header ( top 10 lines ) in tmp_file
#
read first good key value from good_config.txt
while finished = false ; do
   search if key exist in tmp_file
   if yes
      if commented out then
          uncomment it
      fi
      force current good value
   else
      add this current good value at the top of tmp_file
  fi
  read good key value from good_config.txt
  get one ?
  if not
      finished = true
  fi
done

I understand a little with sed and quite nothing with awk and perl.

Help is welcome

Last edited by radoulov; 10-26-2012 at 09:51 AM.. Reason: Additional code tags.
# 2  
Old 10-25-2012
try:
Code:
awk '
  NR==FNR {
  good_value[$1]=$2;
  next;
}
/^ *#/ { next; }
{
  if ($1 in good_value && $2!=good_value[$1]) {print "#" $0; $2=good_value[$1]};
  print $1,$2;
}
' FS="=" good_config.txt OFS="=" config_file > tmp_file


Last edited by rdrtx1; 10-25-2012 at 12:33 PM.. Reason: adding commenting line
# 3  
Old 10-25-2012
@rdrtx1: Good starting point. But I read the request like: If key exists, correct its value; if it does not or is commented out, create/uncomment using correct value.
Try this; anyhow, it does not place good_value at the top:
Code:
awk     'BEGIN {FS=OFS="="}
         NR==FNR {good_value[$1] = $2
                  next
                 }
         {iscomment = gsub (/#/, "", $1)}
         {if ($1 in good_value) {$2 = good_value[$1]; iscomment=0}
          if (!iscomment) print $1,$2;
          delete good_value [$1]
         }          
         END {for (i in good_value) print i, good_value[i]}
        ' good_config.txt config_file

# 4  
Old 10-26-2012
Thank you for helping.
I see that I must dive in AWK.
I will give you news as soon as I understand what your scripts are doing.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Pass config file to bash script

I just want to make sure I am understanding how to pass a config file to a bash script . In the below I pass to arguments to a script, then define them in the script as id and config. I then source config using ., if I understand correctly the variables in the config file can now be used by the... (11 Replies)
Discussion started by: cmccabe
11 Replies

2. Shell Programming and Scripting

Shell script to pass the config file lines as variable on the respective called function on a script

I want to make a config file which contain all the paths. i want to read the config file line by line and pass as an argument on my below function. Replace all the path with reading config path line by line and pass in respective functions. how can i achieve that? Kindly guide. ... (6 Replies)
Discussion started by: sadique.manzar
6 Replies

3. Shell Programming and Scripting

Update config file using shell script in Solaris 5.10

hi I need to update the value in config.txt value using shell script example: lets say a value in config.txt file is as SEQUENCE=1 after some iteration as the loop ends , the SEQUENCE should get update in the config.txt file with a new value of SEQUENCE=2. also , can anyone please help me... (7 Replies)
Discussion started by: ravidwivedi2288
7 Replies

4. Shell Programming and Scripting

bash script config file

hi all config.sh : dhcp="0" setip="1" telnet="1" ping="1" main.sh function dhcp { } function setip { } (1 Reply)
Discussion started by: sadosan83
1 Replies

5. Shell Programming and Scripting

Shell script that will compare two config files and produce 2 outputs 1)actual config file 2)report

Hi I am new to shell scripting. There is a requirement to write a shell script to meet follwing needs.Prompt reply shall be highly appreciated. script that will compare two config files and produce 2 outputs - actual config file and a report indicating changes made. OS :Susi linux ver 10.3. ... (4 Replies)
Discussion started by: muraliinfy04
4 Replies

6. Shell Programming and Scripting

Parse config file data to script variable

I have a script with few pre defined variables. Also have a config file. Something like this. # config file # Define Oracle User MOD1_TAG=abcde MOD2_TAG=xyzabc MOD3_TAG=def I need to parse the config file and have each of the value copied to different variables. Please suggest what... (1 Reply)
Discussion started by: souryadipta
1 Replies

7. Shell Programming and Scripting

Calling Config file in the Script.. Please help

Hi Guys, I need some help related to my script... I will explain you briefly about my script and requirement I written one script which will monitor all my services in the server and send a mail to me.... There are about 10 Services(Processes) running on my services, and I have written 10... (1 Reply)
Discussion started by: Anji
1 Replies

8. Shell Programming and Scripting

need help with calling a config file in script

Hi, I am writting a script and i am using SYBASE database. i have a config file with my logging credentials. when i am calling the file via isql command its throwing me an error. see the below code. while read line do SERVER=`echo $linea |awk -F"|" ' {print $1} ' ` ... (6 Replies)
Discussion started by: dazdseg
6 Replies

9. Shell Programming and Scripting

parsing config file to create new config files

Hi, I want to use a config file as the base file and parse over the values of country and city parameters in the config file and generate separate config files as explained below. I will be using the config file as mentioned below: (config.txt) country:a,b city:1,2 type:b1... (1 Reply)
Discussion started by: clazzic
1 Replies

10. Shell Programming and Scripting

Edit a config file using shell script

I need to edit a config file using shell script. i.e., Search with the 'key' string and edit the 'value'. For eg: below is what I have in the config file "configfile.cfg". Key1=OldValue1 Key2=OldValue2 I want to search for "Key1" and change "OldValue1" to "NewValue1" Thanks for your... (7 Replies)
Discussion started by: rajeshomallur
7 Replies
Login or Register to Ask a Question