This is a continuation of awk gsub not replacing all instances of period in field.
I have some input:
$ cat ./ipv4settings.txt
ipv4.gateway: 192.168.2.1
ipv4.routes: --
ipv4.route-metric: -1
ipv4.route-table: 0 (unspec)
I’m able to generate the desired output (somewhat):
$ awk 'BEGIN{FS=":[[:space:]]+";OFS="="}{gsub("[.]|-","_",$1);$1=$1;print $1"="$2 }' ./ipv4settings.txt
ipv4_gateway=192.168.2.1
ipv4_routes=--
ipv4_route_metric=-1
ipv4_route_table=0 (unspec)
What I want to do next is declare variables for each line of output. I have used several variations using declare:
$awk 'BEGIN{FS=":[[:space:]]+";OFS="="}{gsub("[.]|-","_",$1);$1=$1;print $1"="$2 }' ./ipv4settings.txt
ipv4_gateway=192.168.2.1
ipv4_routes=--
ipv4_route_metric=-1
ipv4_route_table=0 (unspec)
$ declare $( awk 'BEGIN{FS=":[[:space:]]+";OFS="="}{gsub("[.]|-","_",$1);$1=$1;print $1"="$2 }' ./ipv4settings.txt )
-bash: declare: `(unspec)': not a valid identifier
I tried quoting the entire line of output from awk (declare not a valid identifier bash)(https://www.baeldung.com/linux/awk-print-quote-characters):
$ awk 'BEGIN{FS=":[[:space:]]+";OFS="="}{gsub("[.]|-","_",$1);$1=$1;print "\042"$1"="$2"\042" }' ./ipv4settings.txt
"ipv4_gateway=192.168.2.1"
"ipv4_routes=--"
"ipv4_route_metric=-1"
"ipv4_route_table=0 (unspec)"
$ declare $( awk 'BEGIN{FS=":[[:space:]]+";OFS="="}{gsub("[.]|-","_",$1);$1=$1;print "\042"$1"="$2"\042" }' ./ipv4settings.txt )
-bash: declare: `"ipv4_gateway=192.168.2.1"': not a valid identifier
-bash: declare: `"ipv4_routes=--"': not a valid identifier
-bash: declare: `"ipv4_route_metric=-1"': not a valid identifier
-bash: declare: `"ipv4_route_table=0': not a valid identifier
-bash: declare: `(unspec)"': not a valid identifier
… or just the value portion:
$ awk 'BEGIN{FS=":[[:space:]]+";OFS="="}{gsub("[.]|-","_",$1);$1=$1;print $1"=\042"$2"\042" }' ./ipv4settings.txt
ipv4_gateway="192.168.2.1"
ipv4_routes="--"
ipv4_route_metric="-1"
ipv4_route_table="0 (unspec)"
$ declare $( awk 'BEGIN{FS=":[[:space:]]+";OFS="="}{gsub("[.]|-","_",$1);$1=$1;print $1"=\042"$2"\042" }' ./ipv4settings.txt )
-bash: declare: `(unspec)"': not a valid identifier
How do I get declare to work with a variable value with a space?
>Solution :
Another answer to your previous question addresses this (quoting) issue:
$ cat ipv4.awk
BEGIN { sq = "\x27" } # define single quote
/^ipv4[.](gateway|route)/ { # lines that start with ipv4.gateway or ipv4.route
gsub(/[.-]/,"_",$1) # 1st field: replace period/hyphens with underscore
sub(/:[[:space:]]+/,"=" sq) # replace colon + white space with equal sign and single quote
sub(/$/,sq) # append single quote on end of line
print # print current line
$ awk -f ipv4.awk ipv4settings.txt
ipv4_gateway='192.168.2.1'
ipv4_routes='--'
ipv4_route_metric='-1'
ipv4_route_table='0 (unspec)'
To load these variables into your current environment you can redirect the awk results to source, eg:
$ source <(awk -f ipv4.awk ipv4settings.txt)
$ typeset -p ipv4_gateway ipv4_routes ipv4_route_metric ipv4_route_table
declare -- ipv4_gateway="192.168.2.1"
declare -- ipv4_routes="--"
declare -- ipv4_route_metric="-1"
declare -- ipv4_route_table="0 (unspec)"