LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-22-2011, 12:04 PM   #1
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,094

Rep: Reputation: 198Reputation: 198
Editing configuration files with [foo] like sections in them.


Is there a tool which can be used to script the modification of configuration files which look like this
Code:
[Section One]
Name=Some text here
Arbitryvalue=7
Something=seven small dogs

[Section Seven]
Version_Major=8
Version_Mino=7
I don't know what the technical term, if there is one, for that configuration file format is but it's quite common.

Such files can always be modified from a shell script using sed or similar, but it often requires some careful thought to make sure you only modify the right part of the file, that the script doesn't create a mess if run multiple times and other such considerations. I'm wondering if there is a nicer way of doing it. I've looked at augeas but it doesn't appear to understand such files as they don't show up under /files.
 
Old 10-22-2011, 12:22 PM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
You could probably use sed to edit such files in a script. This is often done for these types of files when you install a package.

sed -i '/[Section One]/,/^$/{
/ArbitraryName/s/=.*/=8/
}' configfile

This type of configuration file is common. It was used for Windows 3.1 configuration files, and is mentioned in the "Art of Unix Programming". It is easy to edit in a script using tools like sed or awk.

Another type are files containing name=value pairs that can be sourced as a bash scripts. You can simply execute the config script and use the set variables. One example would be the ifcfg-* configuration files.

A third type are XML files, which require tools like xsltproc to process. They aren't as easy to process but are much more flexible.
 
1 members found this post helpful.
Old 10-23-2011, 04:30 AM   #3
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,094

Original Poster
Rep: Reputation: 198Reputation: 198
Quote:
Originally Posted by arizonagroovejet View Post
Such files can always be modified from a shell script using sed or similar...I'm wondering if there is a nicer way of doing it.

Quote:
Originally Posted by jschiwal View Post
You could probably use sed to edit such files in a script.
That example of using sed you show looks like a lot cleverer than how I've done it though and after reading http://www.grymoire.com/Unix/Sed.html#uh-35 I think I understand what it's doing. So even though it's still using sed, it's an improvement over what I've been doing thus far, thanks.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Killing foo, which I started as foo &. stf92 Linux - Newbie 9 11-08-2010 10:54 PM
Editing configuration files of gnome-panel cmetelko Linux - Desktop 0 02-21-2007 11:05 AM
reading files in C++, in sections WhiskeyTangoFoxtrot Programming 17 05-19-2006 12:14 AM
Catching sections in .conf files with C++ gamehack Programming 3 05-16-2006 08:48 AM
How to make .foo files hidden in Windows underdawg General 2 09-23-2005 03:49 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration