Last active
August 17, 2023 20:41
-
-
Save thomedes/6201620 to your computer and use it in GitHub Desktop.
sed one-liners to deal with .ini / .conf files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# List all [sections] of a .INI file | |
sed -n 's/^[ \t]*\[\(.*\)\].*/\1/p' | |
# Read KEY from [SECTION] | |
sed -n '/^[ \t]*\[SECTION\]/,/\[/s/^[ \t]*KEY[ \t]*=[ \t]*//p' | |
# Read all values from SECTION in a clean KEY=VALUE form | |
sed -n '/^[ \t]*\[SECTION\]/,/\[/s/^[ \t]*\([^#; \t][^ \t=]*\).*=[ \t]*\(.*\)/\1=\2/p' | |
# examples: | |
sed -n 's/^[ \t]*\[\(.*\)\].*/\1/p' /etc/samba/smb.conf | |
sed -n '/^[ \t]*\[global\]/,/\[/s/^[ \t]*workgroup[ \t]*=[ \t]*//p' /etc/samba/smb.conf | |
sed -n '/^[ \t]*\[global\]/,/\[/s/^[ \t]*\([^#; \t][^ \t=]*\).*=[ \t]*\(.*\)/\1=\2/p' /etc/samba/smb.conf |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for this gist. As an expansion to what you've already done here's a bash function for updating a key value pair within a specific section. If the key/value doesn't already exist in the section the script will append it to the end of the section. If the section doesn't exist, the script will create it at the end and add the key/value to it.