Reading properties file in shell script containing “=” character
I have two files param.properties and readParam.sh which reads parameter values from param.properties file
param.properties:
val1=a
val2=b=c;c=d
readParam.sh:
PROPERTY_FILE=p.properties
function getProperty {
PROP_KEY=$1
PROP_VALUE=`cat $PROPERTY_FILE | grep "$PROP_KEY" | cut -d'=' -f2`
echo $PROP_VALUE
}
val1=$(getProperty "val1")
echo val1 : $val1
val2=$(getProperty "val2")
echo val2 : $val2
When I run script readParam.sh I get output as:
val1 : a
val2 : b
Where, val2 gives only half value(it gives only "b" instead of "b=c;c=d")
Please help me with the same
shell properties-file
add a comment |
I have two files param.properties and readParam.sh which reads parameter values from param.properties file
param.properties:
val1=a
val2=b=c;c=d
readParam.sh:
PROPERTY_FILE=p.properties
function getProperty {
PROP_KEY=$1
PROP_VALUE=`cat $PROPERTY_FILE | grep "$PROP_KEY" | cut -d'=' -f2`
echo $PROP_VALUE
}
val1=$(getProperty "val1")
echo val1 : $val1
val2=$(getProperty "val2")
echo val2 : $val2
When I run script readParam.sh I get output as:
val1 : a
val2 : b
Where, val2 gives only half value(it gives only "b" instead of "b=c;c=d")
Please help me with the same
shell properties-file
add a comment |
I have two files param.properties and readParam.sh which reads parameter values from param.properties file
param.properties:
val1=a
val2=b=c;c=d
readParam.sh:
PROPERTY_FILE=p.properties
function getProperty {
PROP_KEY=$1
PROP_VALUE=`cat $PROPERTY_FILE | grep "$PROP_KEY" | cut -d'=' -f2`
echo $PROP_VALUE
}
val1=$(getProperty "val1")
echo val1 : $val1
val2=$(getProperty "val2")
echo val2 : $val2
When I run script readParam.sh I get output as:
val1 : a
val2 : b
Where, val2 gives only half value(it gives only "b" instead of "b=c;c=d")
Please help me with the same
shell properties-file
I have two files param.properties and readParam.sh which reads parameter values from param.properties file
param.properties:
val1=a
val2=b=c;c=d
readParam.sh:
PROPERTY_FILE=p.properties
function getProperty {
PROP_KEY=$1
PROP_VALUE=`cat $PROPERTY_FILE | grep "$PROP_KEY" | cut -d'=' -f2`
echo $PROP_VALUE
}
val1=$(getProperty "val1")
echo val1 : $val1
val2=$(getProperty "val2")
echo val2 : $val2
When I run script readParam.sh I get output as:
val1 : a
val2 : b
Where, val2 gives only half value(it gives only "b" instead of "b=c;c=d")
Please help me with the same
shell properties-file
shell properties-file
asked Nov 21 '18 at 9:27
JAVA_GEEKJAVA_GEEK
154
154
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
just extend your existing codes:
change
cut -d'=' -f2
into:
cut -d'=' -f2-
However I would change your cat|grep|cut
chain into a single awk one-liner:
function getProperty {
awk -F'=' -v k="$1" '$1==k&&sub(/^[^=]*=/,"")' $PROPERTY_FILE
}
add a comment |
PROP_VALUE=`cat $PROPERTY_FILE | grep "$PROP_KEY" | cut -d'=' -f2`
-f2 will give you only the second parameter from the string you're cutting based on the delimiter.
since "=" is present in multiple places in a same line it will split the string in to multiple part and when you try to retrive the output using f2 is will get you only the second part.
so use to get the rest of the values is the string after first occurrence of the delimiter.
PROP_VALUE=`cat $PROPERTY_FILE | grep "$PROP_KEY" | cut -d'=' -f2-`
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53408886%2freading-properties-file-in-shell-script-containing-character%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
just extend your existing codes:
change
cut -d'=' -f2
into:
cut -d'=' -f2-
However I would change your cat|grep|cut
chain into a single awk one-liner:
function getProperty {
awk -F'=' -v k="$1" '$1==k&&sub(/^[^=]*=/,"")' $PROPERTY_FILE
}
add a comment |
just extend your existing codes:
change
cut -d'=' -f2
into:
cut -d'=' -f2-
However I would change your cat|grep|cut
chain into a single awk one-liner:
function getProperty {
awk -F'=' -v k="$1" '$1==k&&sub(/^[^=]*=/,"")' $PROPERTY_FILE
}
add a comment |
just extend your existing codes:
change
cut -d'=' -f2
into:
cut -d'=' -f2-
However I would change your cat|grep|cut
chain into a single awk one-liner:
function getProperty {
awk -F'=' -v k="$1" '$1==k&&sub(/^[^=]*=/,"")' $PROPERTY_FILE
}
just extend your existing codes:
change
cut -d'=' -f2
into:
cut -d'=' -f2-
However I would change your cat|grep|cut
chain into a single awk one-liner:
function getProperty {
awk -F'=' -v k="$1" '$1==k&&sub(/^[^=]*=/,"")' $PROPERTY_FILE
}
edited Nov 21 '18 at 9:45
answered Nov 21 '18 at 9:32
KentKent
146k26158216
146k26158216
add a comment |
add a comment |
PROP_VALUE=`cat $PROPERTY_FILE | grep "$PROP_KEY" | cut -d'=' -f2`
-f2 will give you only the second parameter from the string you're cutting based on the delimiter.
since "=" is present in multiple places in a same line it will split the string in to multiple part and when you try to retrive the output using f2 is will get you only the second part.
so use to get the rest of the values is the string after first occurrence of the delimiter.
PROP_VALUE=`cat $PROPERTY_FILE | grep "$PROP_KEY" | cut -d'=' -f2-`
add a comment |
PROP_VALUE=`cat $PROPERTY_FILE | grep "$PROP_KEY" | cut -d'=' -f2`
-f2 will give you only the second parameter from the string you're cutting based on the delimiter.
since "=" is present in multiple places in a same line it will split the string in to multiple part and when you try to retrive the output using f2 is will get you only the second part.
so use to get the rest of the values is the string after first occurrence of the delimiter.
PROP_VALUE=`cat $PROPERTY_FILE | grep "$PROP_KEY" | cut -d'=' -f2-`
add a comment |
PROP_VALUE=`cat $PROPERTY_FILE | grep "$PROP_KEY" | cut -d'=' -f2`
-f2 will give you only the second parameter from the string you're cutting based on the delimiter.
since "=" is present in multiple places in a same line it will split the string in to multiple part and when you try to retrive the output using f2 is will get you only the second part.
so use to get the rest of the values is the string after first occurrence of the delimiter.
PROP_VALUE=`cat $PROPERTY_FILE | grep "$PROP_KEY" | cut -d'=' -f2-`
PROP_VALUE=`cat $PROPERTY_FILE | grep "$PROP_KEY" | cut -d'=' -f2`
-f2 will give you only the second parameter from the string you're cutting based on the delimiter.
since "=" is present in multiple places in a same line it will split the string in to multiple part and when you try to retrive the output using f2 is will get you only the second part.
so use to get the rest of the values is the string after first occurrence of the delimiter.
PROP_VALUE=`cat $PROPERTY_FILE | grep "$PROP_KEY" | cut -d'=' -f2-`
answered Nov 21 '18 at 9:59
Sekar RamuSekar Ramu
164110
164110
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53408886%2freading-properties-file-in-shell-script-containing-character%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown