How do I parse a file and substitute the environment variables with values in Python
Tag : python , By : user113409
Date : March 29 2020, 07:55 AM
will help you I have a text file (lets call it file1.txt), it contains the following lines: "read model $path_1" , string.Template >>> from string import Template
>>> s = Template('$who likes $what')
>>> s.substitute(who='tim', what='kung pao')
'tim likes kung pao'
|
How to retrieve values from configuration file for Shell Scripts ( Without the knowledge of Keys )
Tag : linux , By : Lee KW
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , Assuming the configuration file only contains var=value declarations with each declaration occupying a single line. configfile=./Configuration.conf
. "$configfile"
declare -A configlist
while IFS='=' read -r key val ; do
# skip empty / commented lines and obviously invalid input
[[ $key =~ ^[[:space:]]*[_[:alpha:]] ]] || continue
# Stripping up to the last space in $key removes "export".
# Using eval to approximate the shell's handling of lines like this:
# var="some thing with spaces" # and a trailing comment.
eval "configlist[${key##* }]=$val"
done < "$configfile"
# The keys are "${!configlist[@]}", the values are "${configlist[@]}"
#
#for key in "${!configlist[@]}" ; do
# printf '"%s" = "%s"\n' "$key" "${configlist[$key]}"
#done
for value in "${configlist[@]}" ; do
: your logic goes here
done
|
Substitute values in a file using lookup file in Shell
Tag : shell , By : Matt Logan
Date : March 29 2020, 07:55 AM
To fix the issue you can do I have a lookup file with contents , give this a try: awk -F',' 'NR==FNR{k[$1]=$2;next}
{for(x in k)gsub(x,k[x]);print}' lookup.file targetfile
A,foo
foo,bar
bar,blah
a.*, B
b-$, foo
|
Storing multiple data with same keys in a file and getting values from shell Script?
Date : March 29 2020, 07:55 AM
To fix the issue you can do It seems you want to access all users with their password from the file inside a shell script. One possible solution could be to store in the properties file the user name as key and the password as value. xxx=xxxxx
yyy=yyyyy
zzz=zzzzz
#!/bin/ksh
while read line.
do
user=${line%%=*}
ip=${line##*=}
# example how to access the values
printf "user: %s IP: %s\n" "${user}" "${ip}"
done < your.properties
user: xxx ip: xxxxx
user: yyy ip: yyyyy
user: zzz ip: zzzzz
user=www
ip=wwwww
user=xxx
ip=xxxxx
user=yyy
ip=yyyyy
user=zzz
ip=zzzzz
#!/bin/sh
# the file input from file `your.properties` is redirected to filedescriptor 3
# to avoid problems with programs inside the loop which themself use `stdin`
# for reading input
while read user <&3; read ip <&3
do
user=${user##*=}
ip=${ip##*=}
# example how to access the values
printf "user: %s ip: %s\n" "${user}" "${ip}"
done 3< your.properties
user: www ip: wwwww
user: xxx ip: xxxxx
user: yyy ip: yyyyy
user: zzz ip: zzzzz
|
Substitute shell variable references in a file with their values
Date : March 29 2020, 07:55 AM
like below fixes the issue I can replace placeholders (named after the variable name) in a file using sed : , You can rewrite your config.sh a little bit to fix the issues: # grab the output file name
file=$1
shift
# loop through the rest of args which are names of variables that
# need to be substituted
for var in "${@}"; do
# note: ${var} and ${!var} do get evaluated inside single quotes so long as
# there are enclosing double quotes around them
sed_expr=${sed_expr}" -e 's/\${$var}/${!var}/'"
done
# enclose sed_expr as well file in double quotes to prevent
# word splitting and globbig
sed -i "$sed_expr" "$file"
export ID="toto" SECRET="tata" OTHER="franck"
./config.sh file ID SECRET OTHER
ID="toto" SECRET="tata" OTHER="franck" ./config.sh file ID SECRET OTHER
-e 's/${ID}/toto/' -e 's/${SECRET}/tata/' -e 's/${OTHER}/franck/'
$clientId = 'toto';
$clientSecret = 'tata';
$publication = 'franck';
|