How do you extract a value from `key=value` or `key: value` lines?
BeginnerConceptText processing: grep, sed, awk, cut, sort, uniq
Answer
grep -oP '^PORT=\K.*' app.env # GNU grep, \K drops the prefix
awk -F= '$1 == "PORT" {print $2}' app.env
sed -n 's/^PORT=//p' app.env
awk -F': *' '$1 == "Version" {print $2}' <(dpkg -s nginx)
Anchor the key (^PORT=) so EXPORT= or SUPPORT_PORT= does not match. For real YAML, JSON or INI use yq, jq or crudini.
More on Text processing: grep, sed, awk, cut, sort, uniq
What are the most useful `grep` flags?BeginnerWhat is the difference between basic, extended and Perl regular expressions in grep?IntermediateWhat are the core `sed` commands you use in automation?BeginnerWhy does `sed -i` behave differently on macOS and Linux?IntermediateHow do you use variables inside a `sed` expression safely?AdvancedWhat is the structure of an `awk` program?Beginner