check and replace alphanumeric pattern using sed
I have a requirement where the input value needs to be checked so that it follows a fixed pattern, if not, fix it to the pattern using sed
.
Final output ONLY needs to be in this pattern env00XX
where XX is from 01 to 49
some inputs I might expect ENV00XX, Env00XX, env1 env2 ... env9, or any other criteria needs to be reset to env0001 to env0049 (lowercase env and 4 digits upto 49)
I have tried many ways closest are
sed -r ":r;s/b[0-9]{1,3}b/0&/g;tr"
gives me 3 to 0003 and 23 to 0023
also echo ENV001 | sed 'y/ENV/env/' | sed -e 's/00*//g'
gives me from ENV001 to env1
But I cannot go further.
bash sed
add a comment |
I have a requirement where the input value needs to be checked so that it follows a fixed pattern, if not, fix it to the pattern using sed
.
Final output ONLY needs to be in this pattern env00XX
where XX is from 01 to 49
some inputs I might expect ENV00XX, Env00XX, env1 env2 ... env9, or any other criteria needs to be reset to env0001 to env0049 (lowercase env and 4 digits upto 49)
I have tried many ways closest are
sed -r ":r;s/b[0-9]{1,3}b/0&/g;tr"
gives me 3 to 0003 and 23 to 0023
also echo ENV001 | sed 'y/ENV/env/' | sed -e 's/00*//g'
gives me from ENV001 to env1
But I cannot go further.
bash sed
Prefix your code/data with four white spaces. Please take a look at editing-help.
– Cyrus
Nov 24 '18 at 0:53
Please take a look at: What should I do when someone answers my question?
– Cyrus
Nov 24 '18 at 8:30
add a comment |
I have a requirement where the input value needs to be checked so that it follows a fixed pattern, if not, fix it to the pattern using sed
.
Final output ONLY needs to be in this pattern env00XX
where XX is from 01 to 49
some inputs I might expect ENV00XX, Env00XX, env1 env2 ... env9, or any other criteria needs to be reset to env0001 to env0049 (lowercase env and 4 digits upto 49)
I have tried many ways closest are
sed -r ":r;s/b[0-9]{1,3}b/0&/g;tr"
gives me 3 to 0003 and 23 to 0023
also echo ENV001 | sed 'y/ENV/env/' | sed -e 's/00*//g'
gives me from ENV001 to env1
But I cannot go further.
bash sed
I have a requirement where the input value needs to be checked so that it follows a fixed pattern, if not, fix it to the pattern using sed
.
Final output ONLY needs to be in this pattern env00XX
where XX is from 01 to 49
some inputs I might expect ENV00XX, Env00XX, env1 env2 ... env9, or any other criteria needs to be reset to env0001 to env0049 (lowercase env and 4 digits upto 49)
I have tried many ways closest are
sed -r ":r;s/b[0-9]{1,3}b/0&/g;tr"
gives me 3 to 0003 and 23 to 0023
also echo ENV001 | sed 'y/ENV/env/' | sed -e 's/00*//g'
gives me from ENV001 to env1
But I cannot go further.
bash sed
bash sed
edited Nov 24 '18 at 0:53
Tico
2,08722432
2,08722432
asked Nov 24 '18 at 0:51
SaiSai
82
82
Prefix your code/data with four white spaces. Please take a look at editing-help.
– Cyrus
Nov 24 '18 at 0:53
Please take a look at: What should I do when someone answers my question?
– Cyrus
Nov 24 '18 at 8:30
add a comment |
Prefix your code/data with four white spaces. Please take a look at editing-help.
– Cyrus
Nov 24 '18 at 0:53
Please take a look at: What should I do when someone answers my question?
– Cyrus
Nov 24 '18 at 8:30
Prefix your code/data with four white spaces. Please take a look at editing-help.
– Cyrus
Nov 24 '18 at 0:53
Prefix your code/data with four white spaces. Please take a look at editing-help.
– Cyrus
Nov 24 '18 at 0:53
Please take a look at: What should I do when someone answers my question?
– Cyrus
Nov 24 '18 at 8:30
Please take a look at: What should I do when someone answers my question?
– Cyrus
Nov 24 '18 at 8:30
add a comment |
1 Answer
1
active
oldest
votes
Try this:
echo 'EnV009' | sed 's/env([0-9]{1,})/env0001/I;s/0*([0-9]{4})/1/'
Explanation: The first sed expression s/(env)([0-9]{1,})/env0002/I
searches case-insensitively for "env" followed by at least one digit. It then replaces the matched text with "env", followed by three zeros, followed by the matched digit pattern (one or more digits).
EnV009 becomes env000009
The second sed expression s/0*([0-9]{4})/1/
takes the output of the first, and slices off the extra zeros before the last four digits.
env000009 becomes env0009
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%2f53454279%2fcheck-and-replace-alphanumeric-pattern-using-sed%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try this:
echo 'EnV009' | sed 's/env([0-9]{1,})/env0001/I;s/0*([0-9]{4})/1/'
Explanation: The first sed expression s/(env)([0-9]{1,})/env0002/I
searches case-insensitively for "env" followed by at least one digit. It then replaces the matched text with "env", followed by three zeros, followed by the matched digit pattern (one or more digits).
EnV009 becomes env000009
The second sed expression s/0*([0-9]{4})/1/
takes the output of the first, and slices off the extra zeros before the last four digits.
env000009 becomes env0009
add a comment |
Try this:
echo 'EnV009' | sed 's/env([0-9]{1,})/env0001/I;s/0*([0-9]{4})/1/'
Explanation: The first sed expression s/(env)([0-9]{1,})/env0002/I
searches case-insensitively for "env" followed by at least one digit. It then replaces the matched text with "env", followed by three zeros, followed by the matched digit pattern (one or more digits).
EnV009 becomes env000009
The second sed expression s/0*([0-9]{4})/1/
takes the output of the first, and slices off the extra zeros before the last four digits.
env000009 becomes env0009
add a comment |
Try this:
echo 'EnV009' | sed 's/env([0-9]{1,})/env0001/I;s/0*([0-9]{4})/1/'
Explanation: The first sed expression s/(env)([0-9]{1,})/env0002/I
searches case-insensitively for "env" followed by at least one digit. It then replaces the matched text with "env", followed by three zeros, followed by the matched digit pattern (one or more digits).
EnV009 becomes env000009
The second sed expression s/0*([0-9]{4})/1/
takes the output of the first, and slices off the extra zeros before the last four digits.
env000009 becomes env0009
Try this:
echo 'EnV009' | sed 's/env([0-9]{1,})/env0001/I;s/0*([0-9]{4})/1/'
Explanation: The first sed expression s/(env)([0-9]{1,})/env0002/I
searches case-insensitively for "env" followed by at least one digit. It then replaces the matched text with "env", followed by three zeros, followed by the matched digit pattern (one or more digits).
EnV009 becomes env000009
The second sed expression s/0*([0-9]{4})/1/
takes the output of the first, and slices off the extra zeros before the last four digits.
env000009 becomes env0009
answered Nov 24 '18 at 1:28
VasanVasan
3,69531231
3,69531231
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%2f53454279%2fcheck-and-replace-alphanumeric-pattern-using-sed%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
Prefix your code/data with four white spaces. Please take a look at editing-help.
– Cyrus
Nov 24 '18 at 0:53
Please take a look at: What should I do when someone answers my question?
– Cyrus
Nov 24 '18 at 8:30