logo
down
shadow

How to substitute values in a key-value file if keys exists in shell environment?


How to substitute values in a key-value file if keys exists in shell environment?

Content Index :

How to substitute values in a key-value file if keys exists in shell environment?
Tag : shell , By : user182548
Date : December 05 2020, 12:18 PM

this one helps. I have a file like below with key value pair separated by =. I have already exported some variables to my shell. Now I want a shell script which can substitute the values for its corresponding keys if those keys are already exist in env variables or exported to shell. , I think, you are looking for something like below.
> cat foo.yaml
PROJECT_NAME=test
DOMAIN_NAME=test.localhost
NETWORK_NAME=traefik-network
ENVIRONMENT=dev
DRUPAL_VERSION=8
MYSQL_HOSTNAME=test.mariadb
MYSQL_DATABASE=drupal
MYSQL_USER=test
MYSQL_PASSWORD=pass
MYSQL_PORT=3306
MYSQL_ROOT_PASSWORD=pass
>
> export ENVIRONMENT=sit
> export DRUPAL_VERSION=10
> export MYSQL_PASSWORD="*****"
>
> perl -lpe ' s/(.*)=(.*)/sprintf("%s=%s","$1",$ENV{$1}? $ENV{$1}:$2)/ge ' foo.yaml
PROJECT_NAME=test
DOMAIN_NAME=test.localhost
NETWORK_NAME=traefik-network
ENVIRONMENT=sit
DRUPAL_VERSION=10
MYSQL_HOSTNAME=test.mariadb
MYSQL_DATABASE=drupal
MYSQL_USER=test
MYSQL_PASSWORD=*****
MYSQL_PORT=3306
MYSQL_ROOT_PASSWORD=pass
>
> perl -i -lpe ' s/(.*)=(.*)/sprintf("%s=%s","$1",$ENV{$1}? $ENV{$1}:$2)/ge ' foo.yaml
> cat foo.yaml
PROJECT_NAME=test
DOMAIN_NAME=test.localhost
NETWORK_NAME=traefik-network
ENVIRONMENT=sit
DRUPAL_VERSION=10
MYSQL_HOSTNAME=test.mariadb
MYSQL_DATABASE=drupal
MYSQL_USER=test
MYSQL_PASSWORD=*****
MYSQL_PORT=3306
MYSQL_ROOT_PASSWORD=pass
>
> awk -F"=" ' { $2=ENVIRON[$1]?ENVIRON[$1]:$2; printf("%s=%s\n",$1,$2) } ' foo.yaml
> awk -F"=" ' { printf("%s=%s\n",$1,ENVIRON[$1]?ENVIRON[$1]:$2) } ' foo.yaml
> awk -F"=" ' { if (!/^\s*$/) $0=sprintf("%s=%s",$1,ENVIRON[$1]?ENVIRON[$1]:$2) }1 ' foo.yaml

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

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?


Tag : java , By : Zelos
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


Tag : bash , By : TBG
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';
Related Posts Related QUESTIONS :
  • How to make a script with apt-get commands?
  • Combine multiple line which having same start pattern
  • Scope of variables executing functions in a subshell
  • Shell session exited by itself after issuing command "exec<>5"
  • Create new file containing filename and count each file
  • Parsing Nagios configuration file
  • run commands using default.nix file
  • What is the meaning about "xclip -sel"?
  • Why does sed behave differently in a shell script?
  • wait for the service to be stopped and proceed further in shell script
  • Linux shell script to copy and rename multiple files
  • while loop integer expression expected
  • How to redirect the output of csh script that runs in background
  • How do I set a variable to an expr using other predefined variables in UNIX?
  • How to compare latest two files are identical or not with shell?
  • notify-send doesn't work over SSH
  • gitlab-ci.yml not executing shell script
  • Memory device output in shell modification
  • How to use attributes in an Inspec command?
  • I want to compare * as a literal by passin g it as command line argument
  • How do I use multiple decision statement in shell?
  • What is the location of Ubuntu website files on Windows 10?
  • How to find the max value in array without using sort cmd in shell script
  • Storing the new format of converted Image
  • (${var//=/ }) Doesn't work within Alpine Linux while works fine within zsh. Is there similar alternative?
  • Searching a CSV File Using Grep
  • How to handle bash: syntax error near unexpected token `('?
  • Is there a command similar to iwevent to monitor eth0 state?
  • convert the shell output to float64?
  • I need to replace literal \n with newline in shell
  • How to create a unix script to loop a Hive SELECT query by taking table names as input from a file?
  • Why is it much slower to use cut than awk to intercept two strings from each line in a file?
  • How to portably implement `test path1 -ef path2`?
  • how to get minimum prefix from a large file
  • AWK: Read a File "x" and compare it's values against the value of Column 1 and column 2 of file "y"
  • perl encode_base64 versus Redhat base64 command
  • Find all combinations of two-character letters/digits, similar to jot(1) for numbers
  • Command that is not found in Mac os Terminal
  • Rename command not evaluating expressions
  • Stop and Remove Container does not work sometimes
  • Non delimited file on delimited file
  • Using sed to delete all lines between two matching patterns including the matched lines
  • Need uniq output for df -h and remove duliplicate values
  • Filter timestamps in a stream of text for those within last 5 minutes
  • Opening an erlang shell from bash
  • How to escape $ in a echo command nested inside sed command
  • Problem in Makefile forloop break statement
  • If I'm creating 10 compute instances through a script on GCP, will those instances get created sequentially or parallely
  • shell script not working for or condition with -z
  • What does pri/xit mean in autosys job summary?
  • how to remove all files from hdfs location except one?
  • Convert country code to country name from shell script
  • How to optimize grep
  • sed/awk unix csv file modification
  • AWK case sensitive
  • insert single and double inverted comma in between number and char value in shell
  • How to split stdout into stdout and stderr with grep?
  • Generating random characters in a shell script
  • Getting awk syntax error while running belwo script
  • Split the line including all words and special characters
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com