Views:
269
Votes: 2
Tag :
bash
Link:
🔍 See Original Answer on Ask Ubuntu ⧉ 🔗
URL:
https://askubuntu.com/q/1224349
Title:
Variable substitution in Linux and dynamically fetch property from Source file
ID:
/2020/04/05/Variable-substitution-in-Linux-and-dynamically-fetch-property-from-Source-file
Created:
April 5, 2020
Upload:
September 15, 2024
Layout: post
TOC:
false
Navigation: false
Copy to clipboard: false
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