Saturday, February 5, 2022

[SOLVED] How to separate a string with the key/value pairs for different configurations?

Issue

The output below is what I get when I run alias impala (except that it is on one line without the backslashes). However, I would like to extract the bold information and assign it to variables dynamically while reading this information from a text file. I need help or a pattern command to extract the information and assign it to variables which will also be used for other purposes.

alias impala = 'impala-shell -i **<serverinfowith.net.net>** --database=**abcd** \
--kerberos --kerberos_service_name=**xyzq** --ssl \
--ca_cert=**/ab/cd/ef/gh/abc.pem** --query_option=ABC_SYNC=1'

Solution

set -- $(sed 's/.*-i \([^[:space:]]*\) --database=\([^[:space:]]*\) .*_name=\([^[:space:]]*\) --ssl --ca_cert=\([^[:space:]]*\).*/\1 \2 \3 \4/' filename)
server="$1"
dbase="$2"
service="$3"
cert="$4"

The regex is gruesome but captures the four strings you're interested in between \( and \) each time. You can reduce the context if you prefer. You could write [^ ] instead of [^[:space:]] each time, as long as you're confident no-one will use any tabs in the file. The filename is the file containing the string (alias command) shown in the question. The set -- sets the positional arguments ($1..$4) from the output from the sed command. The assignments give names to the values in the positional arguments.



Answered By - Jonathan Leffler
Answer Checked By - Senaida (WPSolving Volunteer)