如何用Grep命令从Kafka实例描述结果提取bootstrap_server_host值
Extract bootstrap_server_host value from Kafka describe output
Here are a few ways to get just the value of bootstrap_server_host from the JSON output:
Using grep with PCRE (Perl-compatible regex)
If your grep supports the -P flag (common in GNU grep), use this one-liner to directly extract the value:
rhoas kafka describe | grep -oP '(?<="bootstrap_server_host": ")[^"]+'
-o: Outputs only the matched part of the line, not the entire line.-P: Enables PCRE, allowing lookbehind assertions to target text right after the key's quoted string.- The regex captures all characters after the key until the next double quote.
Using grep + sed (for systems without PCRE support)
If -P isn't available, combine grep with sed to strip extra content from the line:
rhoas kafka describe | grep bootstrap_server_host | sed 's/.*"bootstrap_server_host": "\([^"]*\)".*/\1/'
- Grep isolates the line containing the target key.
- Sed uses a regex to capture the value inside quotes and replaces the entire line with that captured group.
Better alternative: Use jq (JSON parser)
For reliable JSON handling (works with varying whitespace, nested structures, etc.), use jq instead of grep/sed. This is the most robust approach for JSON data:
rhoas kafka describe | jq -r '.bootstrap_server_host'
-r: Outputs the raw string without surrounding quotes.
内容的提问来源于stack exchange,提问作者comper
相关产品推荐
相关产品推荐

