Replacing settings in config files.


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Replacing settings in config files.
# 1  
Old 11-27-2017
Display Replacing settings in config files.

Hi folks.

I'm a programmer of too many years to mention, but I'm only just starting my journey into unix based systems. I'm staring out with the Raspberry pi as they're inexpensive and have the built in GPIO which is always good fun. It's running the default Raspbian system.

Unix scripting is completely new to me, while I'm geo'll run atting used to it slowly I'm struggling with one problem, I'm not even sure if it's the right thing to do.

I want to change settings in a conf file. Example:
Code:
A Setting=5
Another Setting=15
The Last Setting=42

I'll run a sh to change Another Setting to 27.


Now, I can achieve this in two ways, I can have a bunch of files with different settings and rename/copy the one I want at the time. Or I can search the file and change the setting. The second is the preferred method as it's much more flexible.

How practical is it to write script that will search a file for settings and change them? Is parsing files something that's often done with scripts, if so, what commands do I need to get acquainted with? Or do people jump to python or other methods for such things?

All input welcome.
Thanks.


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 11-27-2017 at 11:06 AM.. Reason: Added CODE tags.
# 2  
Old 11-27-2017
Welcome to the forum.

*nix systems have a wealth of tools to process text files including all sorts of config files. Tools comprise sed, awk, perl to edit files, but also cut, paste, fold to dissect and reassemble them. This is only a short list of examples. On top, many modern shells are progammable and you can cobble a quick band aid script together to do for you what you need.
# 3  
Old 11-27-2017
With shell builtins (no external command awk, sed, perl)
here is a standard procedure
Code:
fn="file.conf"
# split on =, change the certain value, write to .new file:
while IFS="=" read key val
do
  case $key in
  "Another Setting") val=27;;
  esac
  printf "%s\n" "$key=$val"
done < $fn > $fn.new
# If successful
if [ $? -eq 0 ]
then
  # copy back to the original file; if successful delete the .new file
  cp $fn.new $fn &&
  rm $fn.new
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Windows & DOS: Issues & Discussions

(VS 2008) New build config looking files from other folder build config

Hi Team, My new build configuration always looking for the files from the build where i copied from. please help me to resolve this. I am using Visual studio 2008.It has Qt 4.8. plugins,qml,C++ development I created new debug_new build configuration with additional preprocessor from the... (1 Reply)
Discussion started by: SA_Palani
1 Replies

2. Shell Programming and Scripting

Search a particular config in many files

Hi, Having trouble in our network we would like to implement a second "ip-helper" (to have DHCP redondancy) to do so we have to take all the network elements config (switches and routers) , finds where the particular config is and add a second line. To do so we need to have a list where this... (1 Reply)
Discussion started by: Philalapatte
1 Replies

3. Shell Programming and Scripting

What's the best way to read config files?

(copied directly from my other thread about something else) At the moment, I just use... cat config_file|grep var_name|cut -d'=' -f2 ... which is fine for what I've been doing, but I'm not sure what to do when there are a variable number of entries and I want it go through them like a... (7 Replies)
Discussion started by: ninjaaron
7 Replies

4. Shell Programming and Scripting

using perl config files

Hi, I have 2 perl SubRoutines (sub 1 and sub 2). I created two perl modules for these (Sub1.pm and Sub2.pm). How can I include these in a config file (say Config.cfg) and call the config file in my main script Rename.pl to use the 2 subroutines? Right now here are the content of Config.cfg... (1 Reply)
Discussion started by: CCFP
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

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

7. OS X (Apple)

Terminal.app keeps creating copies of my settings files

Under Leopard, I like to conveniently open Terminal windows onto remote systems. I've created several settings files in Terminal, one for each remote system that I want to access. To open window, I right-click on the Terminal icon in the Dock, expand the "New Window" menu item, and select the... (0 Replies)
Discussion started by: siemsen
0 Replies

8. HP-UX

Config Files

Does anyone have a list of key config files and the respective paths for HP-UX? Or does anyone know where I can get a list similar to this? Thanks, TOdd (0 Replies)
Discussion started by: toddjameslane
0 Replies

9. UNIX for Dummies Questions & Answers

Apache Config Files

Currently our Apache log files are huge, I want to put say a month's time limit on this, then when it hits the end of the month I would like it to start over writing. Does anyone know where the config file is for this and what its called? I also want to do exactly the same on wtmp config (who... (1 Reply)
Discussion started by: Webwitch
1 Replies

10. UNIX for Dummies Questions & Answers

new LAN, where are the config files?

We just moved to a new office so few things changed on our LAN. I am on Solaris and trying to find the files that hold the configuration for connecting to the servers. I changed the IP addresses to reflect the new addresses but still I get this error: “unknown sendmail : unable to qualify my own... (3 Replies)
Discussion started by: softarch
3 Replies
Login or Register to Ask a Question