Using SED to derive the values from a String
Clash Royale CLAN TAG#URR8PPP
I want to use echo "123|456|789" | sed 's/^(.*|)(.*|).*$/1'
I am trying to break the string and get the individual values (without the pipe |).
- Scenario 1: I want to get
123
- Scenario 2: I want to get
456
- Scenario 3: I want to get
789
- Scenario 4: I want to get
123 456 789
linux shell-script sed
add a comment |
I want to use echo "123|456|789" | sed 's/^(.*|)(.*|).*$/1'
I am trying to break the string and get the individual values (without the pipe |).
- Scenario 1: I want to get
123
- Scenario 2: I want to get
456
- Scenario 3: I want to get
789
- Scenario 4: I want to get
123 456 789
linux shell-script sed
2
What exactly are you trying to accomplish?
– DopeGhoti
Jul 7 '17 at 18:21
1
Can you show the input, expected output and the options you have tried? There are a lot of people here who can answer this question, but you need to prove you've tried some options before posting a question here.
– rahul
Jul 7 '17 at 18:41
1
are you missing a '/g' at the end, like inecho "123|456|789" | sed 's/^(.*|)(.*|).*$/1/g'
?
– Jaleks
Jul 7 '17 at 18:51
We can't answer your question without knowing what you're trying to accomplish here and what your expected results are.
– Toby
Jul 7 '17 at 19:05
add a comment |
I want to use echo "123|456|789" | sed 's/^(.*|)(.*|).*$/1'
I am trying to break the string and get the individual values (without the pipe |).
- Scenario 1: I want to get
123
- Scenario 2: I want to get
456
- Scenario 3: I want to get
789
- Scenario 4: I want to get
123 456 789
linux shell-script sed
I want to use echo "123|456|789" | sed 's/^(.*|)(.*|).*$/1'
I am trying to break the string and get the individual values (without the pipe |).
- Scenario 1: I want to get
123
- Scenario 2: I want to get
456
- Scenario 3: I want to get
789
- Scenario 4: I want to get
123 456 789
linux shell-script sed
linux shell-script sed
edited Dec 15 at 21:52
Rui F Ribeiro
38.9k1479129
38.9k1479129
asked Jul 7 '17 at 18:20
AlluSingh
549
549
2
What exactly are you trying to accomplish?
– DopeGhoti
Jul 7 '17 at 18:21
1
Can you show the input, expected output and the options you have tried? There are a lot of people here who can answer this question, but you need to prove you've tried some options before posting a question here.
– rahul
Jul 7 '17 at 18:41
1
are you missing a '/g' at the end, like inecho "123|456|789" | sed 's/^(.*|)(.*|).*$/1/g'
?
– Jaleks
Jul 7 '17 at 18:51
We can't answer your question without knowing what you're trying to accomplish here and what your expected results are.
– Toby
Jul 7 '17 at 19:05
add a comment |
2
What exactly are you trying to accomplish?
– DopeGhoti
Jul 7 '17 at 18:21
1
Can you show the input, expected output and the options you have tried? There are a lot of people here who can answer this question, but you need to prove you've tried some options before posting a question here.
– rahul
Jul 7 '17 at 18:41
1
are you missing a '/g' at the end, like inecho "123|456|789" | sed 's/^(.*|)(.*|).*$/1/g'
?
– Jaleks
Jul 7 '17 at 18:51
We can't answer your question without knowing what you're trying to accomplish here and what your expected results are.
– Toby
Jul 7 '17 at 19:05
2
2
What exactly are you trying to accomplish?
– DopeGhoti
Jul 7 '17 at 18:21
What exactly are you trying to accomplish?
– DopeGhoti
Jul 7 '17 at 18:21
1
1
Can you show the input, expected output and the options you have tried? There are a lot of people here who can answer this question, but you need to prove you've tried some options before posting a question here.
– rahul
Jul 7 '17 at 18:41
Can you show the input, expected output and the options you have tried? There are a lot of people here who can answer this question, but you need to prove you've tried some options before posting a question here.
– rahul
Jul 7 '17 at 18:41
1
1
are you missing a '/g' at the end, like in
echo "123|456|789" | sed 's/^(.*|)(.*|).*$/1/g'
?– Jaleks
Jul 7 '17 at 18:51
are you missing a '/g' at the end, like in
echo "123|456|789" | sed 's/^(.*|)(.*|).*$/1/g'
?– Jaleks
Jul 7 '17 at 18:51
We can't answer your question without knowing what you're trying to accomplish here and what your expected results are.
– Toby
Jul 7 '17 at 19:05
We can't answer your question without knowing what you're trying to accomplish here and what your expected results are.
– Toby
Jul 7 '17 at 19:05
add a comment |
4 Answers
4
active
oldest
votes
Since I am not sure what you want this to do, I will confine my answer to fixing the command. It should be:
echo "123|456|789" | sed 's/^(.|)(.|).*$/1/'
(note the / immediately before the last quote character).
This then gives the output:
123|
Is that what you wanted?
add a comment |
echo '123|456|789' | sed 's/|.*//'
echo '123|456|789' | sed 's/^[0-9]*|//;s/|.*//'
echo '123|456|789' | sed 's/.*|//'
echo '123|456|789' | sed 's/|/ /g'
Or if you're not precious about using sed
echo '123|456|789' | cut -f1 -d|
echo '123|456|789' | cut -f2 -d|
echo '123|456|789' | cut -f3 -d|
echo '123|456|789' | cut -f1,2,3 --output-delimiter=" " -d|
add a comment |
My previous answer just fixed an error. Here is the way I would actually solve it:
Scenario 4 is the most complicated, so here is the solution:
echo '123|456|789' | sed 's/([0-9]*)|([0-9]*)|([0-9]*)/1 2 3/'
The 1, 2 and 3 at the end select the part matched between the 'decorated brackets' ( the ( and ) ). Each set of those is referenced by the next number, so 1 for the first one, etc.
For scenarios 1, 2 and 3, you could simplify that, but its easier just to cut and paste, and then just have 1, 2 or 3 as required between the last set of //. It won't hurt to leave the rest of the decorated brackets in there.
This solution can be expanded to any number of fields, although I'd probably rather use cut if possible.
add a comment |
With GNU sed you can capture your fields by mentioning their number in shell var $n
n=2; # to get field number 2
echo "123|456|789" | sed -n "s/|/n/$n;s/^[^n]*|//;P"
while for scenario-4 is:
echo ... | sed -e 'y/|/ /'
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%2f376054%2fusing-sed-to-derive-the-values-from-a-string%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
Since I am not sure what you want this to do, I will confine my answer to fixing the command. It should be:
echo "123|456|789" | sed 's/^(.|)(.|).*$/1/'
(note the / immediately before the last quote character).
This then gives the output:
123|
Is that what you wanted?
add a comment |
Since I am not sure what you want this to do, I will confine my answer to fixing the command. It should be:
echo "123|456|789" | sed 's/^(.|)(.|).*$/1/'
(note the / immediately before the last quote character).
This then gives the output:
123|
Is that what you wanted?
add a comment |
Since I am not sure what you want this to do, I will confine my answer to fixing the command. It should be:
echo "123|456|789" | sed 's/^(.|)(.|).*$/1/'
(note the / immediately before the last quote character).
This then gives the output:
123|
Is that what you wanted?
Since I am not sure what you want this to do, I will confine my answer to fixing the command. It should be:
echo "123|456|789" | sed 's/^(.|)(.|).*$/1/'
(note the / immediately before the last quote character).
This then gives the output:
123|
Is that what you wanted?
answered Jul 7 '17 at 19:17
Bob Eager
1,8861421
1,8861421
add a comment |
add a comment |
echo '123|456|789' | sed 's/|.*//'
echo '123|456|789' | sed 's/^[0-9]*|//;s/|.*//'
echo '123|456|789' | sed 's/.*|//'
echo '123|456|789' | sed 's/|/ /g'
Or if you're not precious about using sed
echo '123|456|789' | cut -f1 -d|
echo '123|456|789' | cut -f2 -d|
echo '123|456|789' | cut -f3 -d|
echo '123|456|789' | cut -f1,2,3 --output-delimiter=" " -d|
add a comment |
echo '123|456|789' | sed 's/|.*//'
echo '123|456|789' | sed 's/^[0-9]*|//;s/|.*//'
echo '123|456|789' | sed 's/.*|//'
echo '123|456|789' | sed 's/|/ /g'
Or if you're not precious about using sed
echo '123|456|789' | cut -f1 -d|
echo '123|456|789' | cut -f2 -d|
echo '123|456|789' | cut -f3 -d|
echo '123|456|789' | cut -f1,2,3 --output-delimiter=" " -d|
add a comment |
echo '123|456|789' | sed 's/|.*//'
echo '123|456|789' | sed 's/^[0-9]*|//;s/|.*//'
echo '123|456|789' | sed 's/.*|//'
echo '123|456|789' | sed 's/|/ /g'
Or if you're not precious about using sed
echo '123|456|789' | cut -f1 -d|
echo '123|456|789' | cut -f2 -d|
echo '123|456|789' | cut -f3 -d|
echo '123|456|789' | cut -f1,2,3 --output-delimiter=" " -d|
echo '123|456|789' | sed 's/|.*//'
echo '123|456|789' | sed 's/^[0-9]*|//;s/|.*//'
echo '123|456|789' | sed 's/.*|//'
echo '123|456|789' | sed 's/|/ /g'
Or if you're not precious about using sed
echo '123|456|789' | cut -f1 -d|
echo '123|456|789' | cut -f2 -d|
echo '123|456|789' | cut -f3 -d|
echo '123|456|789' | cut -f1,2,3 --output-delimiter=" " -d|
answered Jul 7 '17 at 21:09
steve
13.9k22452
13.9k22452
add a comment |
add a comment |
My previous answer just fixed an error. Here is the way I would actually solve it:
Scenario 4 is the most complicated, so here is the solution:
echo '123|456|789' | sed 's/([0-9]*)|([0-9]*)|([0-9]*)/1 2 3/'
The 1, 2 and 3 at the end select the part matched between the 'decorated brackets' ( the ( and ) ). Each set of those is referenced by the next number, so 1 for the first one, etc.
For scenarios 1, 2 and 3, you could simplify that, but its easier just to cut and paste, and then just have 1, 2 or 3 as required between the last set of //. It won't hurt to leave the rest of the decorated brackets in there.
This solution can be expanded to any number of fields, although I'd probably rather use cut if possible.
add a comment |
My previous answer just fixed an error. Here is the way I would actually solve it:
Scenario 4 is the most complicated, so here is the solution:
echo '123|456|789' | sed 's/([0-9]*)|([0-9]*)|([0-9]*)/1 2 3/'
The 1, 2 and 3 at the end select the part matched between the 'decorated brackets' ( the ( and ) ). Each set of those is referenced by the next number, so 1 for the first one, etc.
For scenarios 1, 2 and 3, you could simplify that, but its easier just to cut and paste, and then just have 1, 2 or 3 as required between the last set of //. It won't hurt to leave the rest of the decorated brackets in there.
This solution can be expanded to any number of fields, although I'd probably rather use cut if possible.
add a comment |
My previous answer just fixed an error. Here is the way I would actually solve it:
Scenario 4 is the most complicated, so here is the solution:
echo '123|456|789' | sed 's/([0-9]*)|([0-9]*)|([0-9]*)/1 2 3/'
The 1, 2 and 3 at the end select the part matched between the 'decorated brackets' ( the ( and ) ). Each set of those is referenced by the next number, so 1 for the first one, etc.
For scenarios 1, 2 and 3, you could simplify that, but its easier just to cut and paste, and then just have 1, 2 or 3 as required between the last set of //. It won't hurt to leave the rest of the decorated brackets in there.
This solution can be expanded to any number of fields, although I'd probably rather use cut if possible.
My previous answer just fixed an error. Here is the way I would actually solve it:
Scenario 4 is the most complicated, so here is the solution:
echo '123|456|789' | sed 's/([0-9]*)|([0-9]*)|([0-9]*)/1 2 3/'
The 1, 2 and 3 at the end select the part matched between the 'decorated brackets' ( the ( and ) ). Each set of those is referenced by the next number, so 1 for the first one, etc.
For scenarios 1, 2 and 3, you could simplify that, but its easier just to cut and paste, and then just have 1, 2 or 3 as required between the last set of //. It won't hurt to leave the rest of the decorated brackets in there.
This solution can be expanded to any number of fields, although I'd probably rather use cut if possible.
answered Jul 8 '17 at 16:43
Bob Eager
1,8861421
1,8861421
add a comment |
add a comment |
With GNU sed you can capture your fields by mentioning their number in shell var $n
n=2; # to get field number 2
echo "123|456|789" | sed -n "s/|/n/$n;s/^[^n]*|//;P"
while for scenario-4 is:
echo ... | sed -e 'y/|/ /'
add a comment |
With GNU sed you can capture your fields by mentioning their number in shell var $n
n=2; # to get field number 2
echo "123|456|789" | sed -n "s/|/n/$n;s/^[^n]*|//;P"
while for scenario-4 is:
echo ... | sed -e 'y/|/ /'
add a comment |
With GNU sed you can capture your fields by mentioning their number in shell var $n
n=2; # to get field number 2
echo "123|456|789" | sed -n "s/|/n/$n;s/^[^n]*|//;P"
while for scenario-4 is:
echo ... | sed -e 'y/|/ /'
With GNU sed you can capture your fields by mentioning their number in shell var $n
n=2; # to get field number 2
echo "123|456|789" | sed -n "s/|/n/$n;s/^[^n]*|//;P"
while for scenario-4 is:
echo ... | sed -e 'y/|/ /'
answered Jul 8 '17 at 17:26
user218374
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%2f376054%2fusing-sed-to-derive-the-values-from-a-string%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
2
What exactly are you trying to accomplish?
– DopeGhoti
Jul 7 '17 at 18:21
1
Can you show the input, expected output and the options you have tried? There are a lot of people here who can answer this question, but you need to prove you've tried some options before posting a question here.
– rahul
Jul 7 '17 at 18:41
1
are you missing a '/g' at the end, like in
echo "123|456|789" | sed 's/^(.*|)(.*|).*$/1/g'
?– Jaleks
Jul 7 '17 at 18:51
We can't answer your question without knowing what you're trying to accomplish here and what your expected results are.
– Toby
Jul 7 '17 at 19:05