Bash one liner to change configuration parameters
I have a config file with following structure.
ValueOne = 1
ValueTwo = 2
ValueThree = 3
I want a one liner bash script to find ValueTwo and change the value to 22222.
Any idea?
Not School Thing
shell-script text-processing
add a comment |
I have a config file with following structure.
ValueOne = 1
ValueTwo = 2
ValueThree = 3
I want a one liner bash script to find ValueTwo and change the value to 22222.
Any idea?
Not School Thing
shell-script text-processing
add a comment |
I have a config file with following structure.
ValueOne = 1
ValueTwo = 2
ValueThree = 3
I want a one liner bash script to find ValueTwo and change the value to 22222.
Any idea?
Not School Thing
shell-script text-processing
I have a config file with following structure.
ValueOne = 1
ValueTwo = 2
ValueThree = 3
I want a one liner bash script to find ValueTwo and change the value to 22222.
Any idea?
Not School Thing
shell-script text-processing
shell-script text-processing
edited Jul 16 '14 at 14:08
terdon♦
128k31249423
128k31249423
asked Jul 16 '14 at 13:36
TRA
2314
2314
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
I bet there'll be better ones but here's my go:
If the config file has parameters on their own line
sed -i '/ValueTwo/s/= .*/= 22222/' config_file
/ValueTwo/
: Search for the stringValueTwo
to find which line to operate on (Addresses)
s/= .*/= 22222/
: On the lines that match the search above, substitute= .*
for= 22222
(Substitute)
= .*
: Search for the=
character followed by a space () character followed by 0 or more of any character (
.*
) (Regex example)
= 22222
: Replace what's found with the literal string= 22222
This will replace the contents of config_file in-place. To create a new file with the parameter changed, remove -i
and place > new_file
at the end of the line.
If your config file has parameters on the same line (like the unedited question):
sed -i 's/ValueTwo = [^ ]*/ValueTwo = 22222/' config_file
This will replace the contents of config_file in-place as well. It will work as long as there are no spaces in the parameter for ValueTwo. This will also work in the case where parameters are on their own line, but the former method is perhaps more robust in that case.
4
I think you lost your bet.
– mikeserv
Sep 9 '14 at 16:05
1
That is some RegEx'ish mumbo jumbo, would love an explanation/s/=
?
– Ray Foss
May 17 '16 at 14:16
add a comment |
perl -p -i.bak -e 's/ValueTwo = 2/ValueTwo = 22222/' path/to/configfile
will edit the file in-place and save a copy of the original in case of finger trouble. You can do the same with awk.
add a comment |
Assuming ValueTwo
is a number, sed
will do just fine:
sed -e 's/ValueTwo = [0-9]*/ValueTwo = 2222/g' your_config_file > output_file
+1 but why would you limit it to numbers? What's wrong with.*
?
– terdon♦
Jul 16 '14 at 14:12
@terdon, in the first version of the question all variables were in one line, separated by whitespaces. Also it is a good practice to limit the scope of regular expression to values that you expect to find - this way you can filter out some unexpected errors - e.g. when a text is logged instead of a number.
– Paweł Rumian
Jul 16 '14 at 14:21
add a comment |
I'd go for awk
:
awk '/ValueTwo/{$3=22222}1;' file > newfile
The above checks whether a given line matches ValueTwo and sets the 3d field to 222
on matching lines. The 1;
is just an awk shgorthand way of writing print $0
, it will print each line. Since it is outside the match block (/ValueTwo/{}
), it will cause all lines to be printed.
Since you asked for a bash solution though (don't know why you would prefer one but still), you could try this:
while read key eq val; do
[ $key = "ValueTwo" ] && val=22222
printf "%s %s %sn" $key $eq $val
done < file
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2funix.stackexchange.com%2fquestions%2f144846%2fbash-one-liner-to-change-configuration-parameters%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
I bet there'll be better ones but here's my go:
If the config file has parameters on their own line
sed -i '/ValueTwo/s/= .*/= 22222/' config_file
/ValueTwo/
: Search for the stringValueTwo
to find which line to operate on (Addresses)
s/= .*/= 22222/
: On the lines that match the search above, substitute= .*
for= 22222
(Substitute)
= .*
: Search for the=
character followed by a space () character followed by 0 or more of any character (
.*
) (Regex example)
= 22222
: Replace what's found with the literal string= 22222
This will replace the contents of config_file in-place. To create a new file with the parameter changed, remove -i
and place > new_file
at the end of the line.
If your config file has parameters on the same line (like the unedited question):
sed -i 's/ValueTwo = [^ ]*/ValueTwo = 22222/' config_file
This will replace the contents of config_file in-place as well. It will work as long as there are no spaces in the parameter for ValueTwo. This will also work in the case where parameters are on their own line, but the former method is perhaps more robust in that case.
4
I think you lost your bet.
– mikeserv
Sep 9 '14 at 16:05
1
That is some RegEx'ish mumbo jumbo, would love an explanation/s/=
?
– Ray Foss
May 17 '16 at 14:16
add a comment |
I bet there'll be better ones but here's my go:
If the config file has parameters on their own line
sed -i '/ValueTwo/s/= .*/= 22222/' config_file
/ValueTwo/
: Search for the stringValueTwo
to find which line to operate on (Addresses)
s/= .*/= 22222/
: On the lines that match the search above, substitute= .*
for= 22222
(Substitute)
= .*
: Search for the=
character followed by a space () character followed by 0 or more of any character (
.*
) (Regex example)
= 22222
: Replace what's found with the literal string= 22222
This will replace the contents of config_file in-place. To create a new file with the parameter changed, remove -i
and place > new_file
at the end of the line.
If your config file has parameters on the same line (like the unedited question):
sed -i 's/ValueTwo = [^ ]*/ValueTwo = 22222/' config_file
This will replace the contents of config_file in-place as well. It will work as long as there are no spaces in the parameter for ValueTwo. This will also work in the case where parameters are on their own line, but the former method is perhaps more robust in that case.
4
I think you lost your bet.
– mikeserv
Sep 9 '14 at 16:05
1
That is some RegEx'ish mumbo jumbo, would love an explanation/s/=
?
– Ray Foss
May 17 '16 at 14:16
add a comment |
I bet there'll be better ones but here's my go:
If the config file has parameters on their own line
sed -i '/ValueTwo/s/= .*/= 22222/' config_file
/ValueTwo/
: Search for the stringValueTwo
to find which line to operate on (Addresses)
s/= .*/= 22222/
: On the lines that match the search above, substitute= .*
for= 22222
(Substitute)
= .*
: Search for the=
character followed by a space () character followed by 0 or more of any character (
.*
) (Regex example)
= 22222
: Replace what's found with the literal string= 22222
This will replace the contents of config_file in-place. To create a new file with the parameter changed, remove -i
and place > new_file
at the end of the line.
If your config file has parameters on the same line (like the unedited question):
sed -i 's/ValueTwo = [^ ]*/ValueTwo = 22222/' config_file
This will replace the contents of config_file in-place as well. It will work as long as there are no spaces in the parameter for ValueTwo. This will also work in the case where parameters are on their own line, but the former method is perhaps more robust in that case.
I bet there'll be better ones but here's my go:
If the config file has parameters on their own line
sed -i '/ValueTwo/s/= .*/= 22222/' config_file
/ValueTwo/
: Search for the stringValueTwo
to find which line to operate on (Addresses)
s/= .*/= 22222/
: On the lines that match the search above, substitute= .*
for= 22222
(Substitute)
= .*
: Search for the=
character followed by a space () character followed by 0 or more of any character (
.*
) (Regex example)
= 22222
: Replace what's found with the literal string= 22222
This will replace the contents of config_file in-place. To create a new file with the parameter changed, remove -i
and place > new_file
at the end of the line.
If your config file has parameters on the same line (like the unedited question):
sed -i 's/ValueTwo = [^ ]*/ValueTwo = 22222/' config_file
This will replace the contents of config_file in-place as well. It will work as long as there are no spaces in the parameter for ValueTwo. This will also work in the case where parameters are on their own line, but the former method is perhaps more robust in that case.
edited 5 mins ago
gene_wood
1054
1054
answered Jul 16 '14 at 13:47
drs
3,30352859
3,30352859
4
I think you lost your bet.
– mikeserv
Sep 9 '14 at 16:05
1
That is some RegEx'ish mumbo jumbo, would love an explanation/s/=
?
– Ray Foss
May 17 '16 at 14:16
add a comment |
4
I think you lost your bet.
– mikeserv
Sep 9 '14 at 16:05
1
That is some RegEx'ish mumbo jumbo, would love an explanation/s/=
?
– Ray Foss
May 17 '16 at 14:16
4
4
I think you lost your bet.
– mikeserv
Sep 9 '14 at 16:05
I think you lost your bet.
– mikeserv
Sep 9 '14 at 16:05
1
1
That is some RegEx'ish mumbo jumbo, would love an explanation
/s/=
?– Ray Foss
May 17 '16 at 14:16
That is some RegEx'ish mumbo jumbo, would love an explanation
/s/=
?– Ray Foss
May 17 '16 at 14:16
add a comment |
perl -p -i.bak -e 's/ValueTwo = 2/ValueTwo = 22222/' path/to/configfile
will edit the file in-place and save a copy of the original in case of finger trouble. You can do the same with awk.
add a comment |
perl -p -i.bak -e 's/ValueTwo = 2/ValueTwo = 22222/' path/to/configfile
will edit the file in-place and save a copy of the original in case of finger trouble. You can do the same with awk.
add a comment |
perl -p -i.bak -e 's/ValueTwo = 2/ValueTwo = 22222/' path/to/configfile
will edit the file in-place and save a copy of the original in case of finger trouble. You can do the same with awk.
perl -p -i.bak -e 's/ValueTwo = 2/ValueTwo = 22222/' path/to/configfile
will edit the file in-place and save a copy of the original in case of finger trouble. You can do the same with awk.
answered Jul 16 '14 at 13:45
RedGrittyBrick
1,6601519
1,6601519
add a comment |
add a comment |
Assuming ValueTwo
is a number, sed
will do just fine:
sed -e 's/ValueTwo = [0-9]*/ValueTwo = 2222/g' your_config_file > output_file
+1 but why would you limit it to numbers? What's wrong with.*
?
– terdon♦
Jul 16 '14 at 14:12
@terdon, in the first version of the question all variables were in one line, separated by whitespaces. Also it is a good practice to limit the scope of regular expression to values that you expect to find - this way you can filter out some unexpected errors - e.g. when a text is logged instead of a number.
– Paweł Rumian
Jul 16 '14 at 14:21
add a comment |
Assuming ValueTwo
is a number, sed
will do just fine:
sed -e 's/ValueTwo = [0-9]*/ValueTwo = 2222/g' your_config_file > output_file
+1 but why would you limit it to numbers? What's wrong with.*
?
– terdon♦
Jul 16 '14 at 14:12
@terdon, in the first version of the question all variables were in one line, separated by whitespaces. Also it is a good practice to limit the scope of regular expression to values that you expect to find - this way you can filter out some unexpected errors - e.g. when a text is logged instead of a number.
– Paweł Rumian
Jul 16 '14 at 14:21
add a comment |
Assuming ValueTwo
is a number, sed
will do just fine:
sed -e 's/ValueTwo = [0-9]*/ValueTwo = 2222/g' your_config_file > output_file
Assuming ValueTwo
is a number, sed
will do just fine:
sed -e 's/ValueTwo = [0-9]*/ValueTwo = 2222/g' your_config_file > output_file
answered Jul 16 '14 at 13:51
Paweł Rumian
1,271720
1,271720
+1 but why would you limit it to numbers? What's wrong with.*
?
– terdon♦
Jul 16 '14 at 14:12
@terdon, in the first version of the question all variables were in one line, separated by whitespaces. Also it is a good practice to limit the scope of regular expression to values that you expect to find - this way you can filter out some unexpected errors - e.g. when a text is logged instead of a number.
– Paweł Rumian
Jul 16 '14 at 14:21
add a comment |
+1 but why would you limit it to numbers? What's wrong with.*
?
– terdon♦
Jul 16 '14 at 14:12
@terdon, in the first version of the question all variables were in one line, separated by whitespaces. Also it is a good practice to limit the scope of regular expression to values that you expect to find - this way you can filter out some unexpected errors - e.g. when a text is logged instead of a number.
– Paweł Rumian
Jul 16 '14 at 14:21
+1 but why would you limit it to numbers? What's wrong with
.*
?– terdon♦
Jul 16 '14 at 14:12
+1 but why would you limit it to numbers? What's wrong with
.*
?– terdon♦
Jul 16 '14 at 14:12
@terdon, in the first version of the question all variables were in one line, separated by whitespaces. Also it is a good practice to limit the scope of regular expression to values that you expect to find - this way you can filter out some unexpected errors - e.g. when a text is logged instead of a number.
– Paweł Rumian
Jul 16 '14 at 14:21
@terdon, in the first version of the question all variables were in one line, separated by whitespaces. Also it is a good practice to limit the scope of regular expression to values that you expect to find - this way you can filter out some unexpected errors - e.g. when a text is logged instead of a number.
– Paweł Rumian
Jul 16 '14 at 14:21
add a comment |
I'd go for awk
:
awk '/ValueTwo/{$3=22222}1;' file > newfile
The above checks whether a given line matches ValueTwo and sets the 3d field to 222
on matching lines. The 1;
is just an awk shgorthand way of writing print $0
, it will print each line. Since it is outside the match block (/ValueTwo/{}
), it will cause all lines to be printed.
Since you asked for a bash solution though (don't know why you would prefer one but still), you could try this:
while read key eq val; do
[ $key = "ValueTwo" ] && val=22222
printf "%s %s %sn" $key $eq $val
done < file
add a comment |
I'd go for awk
:
awk '/ValueTwo/{$3=22222}1;' file > newfile
The above checks whether a given line matches ValueTwo and sets the 3d field to 222
on matching lines. The 1;
is just an awk shgorthand way of writing print $0
, it will print each line. Since it is outside the match block (/ValueTwo/{}
), it will cause all lines to be printed.
Since you asked for a bash solution though (don't know why you would prefer one but still), you could try this:
while read key eq val; do
[ $key = "ValueTwo" ] && val=22222
printf "%s %s %sn" $key $eq $val
done < file
add a comment |
I'd go for awk
:
awk '/ValueTwo/{$3=22222}1;' file > newfile
The above checks whether a given line matches ValueTwo and sets the 3d field to 222
on matching lines. The 1;
is just an awk shgorthand way of writing print $0
, it will print each line. Since it is outside the match block (/ValueTwo/{}
), it will cause all lines to be printed.
Since you asked for a bash solution though (don't know why you would prefer one but still), you could try this:
while read key eq val; do
[ $key = "ValueTwo" ] && val=22222
printf "%s %s %sn" $key $eq $val
done < file
I'd go for awk
:
awk '/ValueTwo/{$3=22222}1;' file > newfile
The above checks whether a given line matches ValueTwo and sets the 3d field to 222
on matching lines. The 1;
is just an awk shgorthand way of writing print $0
, it will print each line. Since it is outside the match block (/ValueTwo/{}
), it will cause all lines to be printed.
Since you asked for a bash solution though (don't know why you would prefer one but still), you could try this:
while read key eq val; do
[ $key = "ValueTwo" ] && val=22222
printf "%s %s %sn" $key $eq $val
done < file
answered Jul 16 '14 at 14:11
terdon♦
128k31249423
128k31249423
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- 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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2funix.stackexchange.com%2fquestions%2f144846%2fbash-one-liner-to-change-configuration-parameters%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