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 |
Seems like underscore in the key_name breaks it:
$ sed -n '/^[ \t]*\[default\]/,/\[/s/^[ \t]*\([^#; \t][^ \t=]*\).*=[ \t]*\(.*\)/\1=\2/p' ~/.aws/credentials
aws_access_key_id=*****
aws_secre=*****
region=us-east-1
aws_session_=
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.
# escapes all regex meta characters in a string using Perl's "quotemeta" function
awkconfig_quotemeta(){
local PERL_QUOTEMETA_SCRIPT="print quotemeta(\$ARGV[0]);"
perl -e "${PERL_QUOTEMETA_SCRIPT}" "${1:-/dev/stdin}"
}
awkconfig_update_value(){
local section="$1"
local key="$2"
local value="$3"
awk -v RS="(^|\n)[:blank:]*\\\\[" -v setFound=0 -v sec="$section"\
-v kv="$key=$value" -v secPat="$(awkconfig_quotemeta "$section")" -v subPat="$(awkconfig_quotemeta "$key")"\
'$1~"^[[:blank:]\[]*" secPat "[:blank:]*\]"{ \
s = sub("[:blank:]*" subPat "[:blank:]*=[^#^;^\n]*", kv);\
$0 = $0 ((0 == s)? "\n" kv "\n" : ""); # adding kv to $0;\
secFound=1;\
}\
$1~"^[^;^#^\n^=]*\]"{\
$0 = "[" $0; # adding the record separator "[" back in\
}\
{print $0}\
END{ \
if (secFound == 0){\
# adding a new section if we didnt see it yet\
print "#\n";\
print "[" sec "]\n";\
print kv;\
}\
}' "${4:-/dev/stdin}" | awk NF
# "awk NF" removes blank lines https://stackoverflow.com/questions/10347653/awk-remove-blank-lines
# you could also use "sed '/^[[:space:]]*$/d'" instead of "awk NF"
} # end awkconfig_update_value```
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Set an option. Range limit from section to start of next section, then traditional s// to switch option