We help IT Professionals succeed at work.
Get Started

Easy way to read/set config file values?

jbbarnes asked
Last Modified: 2013-12-26
Can someone recommend an easy way to get a value out of a "standard" config file without a lot of parsing?

Most config files I deal with are some variation of this format:

# This is a comment line
key1 = value1
key2 = value2

Or often without the equals sign.

; This is a comment line
key1   value1
key2   value2

I know I can open the file and parse out anything I need, but it's a lot of extra code. Just wondering if there is a unix utility or BASH command to do it. Something like:

getval -f /etc/myfile.conf -k key1

that will handle most types of config files and can find values and ignore comments.

If not, how about a couple of unix commands piped to each other? If it's anything that can be typed at the prompt, then I could execute it within perl and php scripts and capture the return value.

Naturally, it would also be great to have a one-liner that will update a value in a config file.


Watch Question
Top Expert 2005
This problem has been solved!
Unlock 1 Answer and 5 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE