0

I have a source file say config.properties that contains strings like below

ExchgRate_prop="EDB_NAME=share_exchange SRC_WDS=wds PN=ExchgRate"
Compliance_prop="EDB_NAME=share_compliance SRC_WDS=wca PN=com"
Unitcost_prop=="EDB_NAME=share_unitcost SRC_WDS=wda PN=unit"

I have a shell script that takes project names as input. if i pass ExchgRate to the script i need to get value "EDB_NAME=share_exchange SRC_WDS=wds PN=ExchgRate" If i pass Unitcost then i need to get value "EDB_NAME=share_unitcost SRC_WDS=wda PN=unit"

Any help

1 Answer 1

2

This meets your criteria:

$ cat extract.dat
ExchgRate_prop="EDB_NAME=share_exchange SRC_WDS=wds PN=ExchgRate"
Compliance_prop="EDB_NAME=share_compliance SRC_WDS=wca PN=com"
Unitcost_prop=="EDB_NAME=share_unitcost SRC_WDS=wda PN=unit"

$ grep ExchgRate extract.dat | cut -d'"' -f2
EDB_NAME=share_exchange SRC_WDS=wds PN=ExchgRate

$ grep Unitcost extract.dat | cut -d'"' -f2
EDB_NAME=share_unitcost SRC_WDS=wda PN=unit

To make it easier you can create a script (note the path is for my system, not yours):

#!/bin/bash

# NAME: extract.sh
# PATH: $HOME/askubuntu/
# DESC: Answer for: https://askubuntu.com/questions/1224345/variable-substitution-in-linux-and-dynamically-fetch-property-from-source-file
# PARM: $1 = input file, $2 = field name
# NOTE: Prints result
# DATE: April 5, 2020


grep "$2" "$1" | cut -d'"' -f2

Make the script executable with chmod a+x extract.sh.

Put the script in the path or call it in the current directory with:

$ extract.sh extract.dat Unitcost
EDB_NAME=share_unitcost SRC_WDS=wda PN=unit
1
  • Wonderful, Let me check
    – Aravind P
    Apr 5, 2020 at 17:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .