I want to extract part of a line using sed and backreference

Multi tool use
Clash Royale CLAN TAG#URR8PPP
up vote
-1
down vote
favorite
1.The Greater Company :(Ranked 2)
5.United Taxi Service :(Ranked 3)
31.The Cacao Company :(Ranked 4)
40.Monster Inc. :(Ranked 5)
55.Afala, Industrial Strength :(Ranked 6)
60.The North Company :(Ranked 7)
I want to display just the company number and company name. I do not want the rest of the line to be displayed.
1.The Greater Company
5.United Taxi Service
31.The Cacao Company
40.Monster Inc.
55.Afala, Industrial Strength
60.The North Company
I can do this by sed 's/: .* //' filename.txt. How would I use this pattern with back reference so I can achieve the above result. The pattern that I want to match would be ([0-9]* .* :).
ubuntu sed reference
add a comment |Â
up vote
-1
down vote
favorite
1.The Greater Company :(Ranked 2)
5.United Taxi Service :(Ranked 3)
31.The Cacao Company :(Ranked 4)
40.Monster Inc. :(Ranked 5)
55.Afala, Industrial Strength :(Ranked 6)
60.The North Company :(Ranked 7)
I want to display just the company number and company name. I do not want the rest of the line to be displayed.
1.The Greater Company
5.United Taxi Service
31.The Cacao Company
40.Monster Inc.
55.Afala, Industrial Strength
60.The North Company
I can do this by sed 's/: .* //' filename.txt. How would I use this pattern with back reference so I can achieve the above result. The pattern that I want to match would be ([0-9]* .* :).
ubuntu sed reference
1
It appears that you're removing the ranking, not "extracting the company number and rank"; can you clarify your goal vs your output?
– Jeff Schaller
Feb 16 at 1:13
I edited it. What i meant was just have the result being displayed like that. I can do this with sed by using sed 's/: .*//' filename.txt, but i want to know how to achieve this by using backreference.
– Rashad
Feb 16 at 1:17
When somebody asks “How do I do X?â€Â, we always ask “What have you tried?â€Â  Have you read any documentation?  What part of it did you not understand?  And is that a:)
smiley face at the end of your question?  Or are you saying that the pattern that you want to match is[0-9]* .* :
, but you’re putting it in parentheses instead of quotes — this, when the input text contains parentheses (but not quotes).  That is very confusing. … (Cont’d)
– G-Man
Feb 16 at 4:37
(Cont’d) …  What’s more confusing: you say that you want to match[0-9]*  …
, but your data don’t have a space after the number. And you say that you want to match… :
, but your sample output shows that you don’t want to match the colon (:
), just everything up to it.
– G-Man
Feb 16 at 4:37
add a comment |Â
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
1.The Greater Company :(Ranked 2)
5.United Taxi Service :(Ranked 3)
31.The Cacao Company :(Ranked 4)
40.Monster Inc. :(Ranked 5)
55.Afala, Industrial Strength :(Ranked 6)
60.The North Company :(Ranked 7)
I want to display just the company number and company name. I do not want the rest of the line to be displayed.
1.The Greater Company
5.United Taxi Service
31.The Cacao Company
40.Monster Inc.
55.Afala, Industrial Strength
60.The North Company
I can do this by sed 's/: .* //' filename.txt. How would I use this pattern with back reference so I can achieve the above result. The pattern that I want to match would be ([0-9]* .* :).
ubuntu sed reference
1.The Greater Company :(Ranked 2)
5.United Taxi Service :(Ranked 3)
31.The Cacao Company :(Ranked 4)
40.Monster Inc. :(Ranked 5)
55.Afala, Industrial Strength :(Ranked 6)
60.The North Company :(Ranked 7)
I want to display just the company number and company name. I do not want the rest of the line to be displayed.
1.The Greater Company
5.United Taxi Service
31.The Cacao Company
40.Monster Inc.
55.Afala, Industrial Strength
60.The North Company
I can do this by sed 's/: .* //' filename.txt. How would I use this pattern with back reference so I can achieve the above result. The pattern that I want to match would be ([0-9]* .* :).
ubuntu sed reference
edited Feb 28 at 22:33
Drakonoved
674518
674518
asked Feb 16 at 1:08
Rashad
568
568
1
It appears that you're removing the ranking, not "extracting the company number and rank"; can you clarify your goal vs your output?
– Jeff Schaller
Feb 16 at 1:13
I edited it. What i meant was just have the result being displayed like that. I can do this with sed by using sed 's/: .*//' filename.txt, but i want to know how to achieve this by using backreference.
– Rashad
Feb 16 at 1:17
When somebody asks “How do I do X?â€Â, we always ask “What have you tried?â€Â  Have you read any documentation?  What part of it did you not understand?  And is that a:)
smiley face at the end of your question?  Or are you saying that the pattern that you want to match is[0-9]* .* :
, but you’re putting it in parentheses instead of quotes — this, when the input text contains parentheses (but not quotes).  That is very confusing. … (Cont’d)
– G-Man
Feb 16 at 4:37
(Cont’d) …  What’s more confusing: you say that you want to match[0-9]*  …
, but your data don’t have a space after the number. And you say that you want to match… :
, but your sample output shows that you don’t want to match the colon (:
), just everything up to it.
– G-Man
Feb 16 at 4:37
add a comment |Â
1
It appears that you're removing the ranking, not "extracting the company number and rank"; can you clarify your goal vs your output?
– Jeff Schaller
Feb 16 at 1:13
I edited it. What i meant was just have the result being displayed like that. I can do this with sed by using sed 's/: .*//' filename.txt, but i want to know how to achieve this by using backreference.
– Rashad
Feb 16 at 1:17
When somebody asks “How do I do X?â€Â, we always ask “What have you tried?â€Â  Have you read any documentation?  What part of it did you not understand?  And is that a:)
smiley face at the end of your question?  Or are you saying that the pattern that you want to match is[0-9]* .* :
, but you’re putting it in parentheses instead of quotes — this, when the input text contains parentheses (but not quotes).  That is very confusing. … (Cont’d)
– G-Man
Feb 16 at 4:37
(Cont’d) …  What’s more confusing: you say that you want to match[0-9]*  …
, but your data don’t have a space after the number. And you say that you want to match… :
, but your sample output shows that you don’t want to match the colon (:
), just everything up to it.
– G-Man
Feb 16 at 4:37
1
1
It appears that you're removing the ranking, not "extracting the company number and rank"; can you clarify your goal vs your output?
– Jeff Schaller
Feb 16 at 1:13
It appears that you're removing the ranking, not "extracting the company number and rank"; can you clarify your goal vs your output?
– Jeff Schaller
Feb 16 at 1:13
I edited it. What i meant was just have the result being displayed like that. I can do this with sed by using sed 's/: .*//' filename.txt, but i want to know how to achieve this by using backreference.
– Rashad
Feb 16 at 1:17
I edited it. What i meant was just have the result being displayed like that. I can do this with sed by using sed 's/: .*//' filename.txt, but i want to know how to achieve this by using backreference.
– Rashad
Feb 16 at 1:17
When somebody asks “How do I do X?â€Â, we always ask “What have you tried?â€Â  Have you read any documentation?  What part of it did you not understand?  And is that a
:)
smiley face at the end of your question?  Or are you saying that the pattern that you want to match is [0-9]* .* :
, but you’re putting it in parentheses instead of quotes — this, when the input text contains parentheses (but not quotes).  That is very confusing. … (Cont’d)– G-Man
Feb 16 at 4:37
When somebody asks “How do I do X?â€Â, we always ask “What have you tried?â€Â  Have you read any documentation?  What part of it did you not understand?  And is that a
:)
smiley face at the end of your question?  Or are you saying that the pattern that you want to match is [0-9]* .* :
, but you’re putting it in parentheses instead of quotes — this, when the input text contains parentheses (but not quotes).  That is very confusing. … (Cont’d)– G-Man
Feb 16 at 4:37
(Cont’d) …  What’s more confusing: you say that you want to match
[0-9]*  …
, but your data don’t have a space after the number. And you say that you want to match … :
, but your sample output shows that you don’t want to match the colon (:
), just everything up to it.– G-Man
Feb 16 at 4:37
(Cont’d) …  What’s more confusing: you say that you want to match
[0-9]*  …
, but your data don’t have a space after the number. And you say that you want to match … :
, but your sample output shows that you don’t want to match the colon (:
), just everything up to it.– G-Man
Feb 16 at 4:37
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
If all of the lines begin with [0-9]*
there's no need to match it explicitly: just capture up to (but not including) the :
delimiter
sed 's/(^[^:]*) :.*/1/' file
1.The Greater Company
5.United Taxi Service
31.The Cacao Company
40.Monster Inc.
55.Afala, Industrial Strength
60.The North Company
could you explain the command and how the back referencing works
– Rashad
Feb 16 at 1:28
@Rashad the pattern between(
and)
is captured, and then it is backreferenced as1
. See for example Regular Expression Reference: Capturing Groups and Backreferences
– steeldriver
Feb 16 at 1:34
You can omit the second colon:sed 's/(^[^:]*) .*/1/'
– dessert
Feb 16 at 7:44
add a comment |Â
up vote
-2
down vote
grep -Pioe 's*d+.s*(w+[.,]? )+:' file
The question asks how to do this with back reference.   Does your answer use back reference?  Can you explain how?
– G-Man
Feb 16 at 4:22
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
If all of the lines begin with [0-9]*
there's no need to match it explicitly: just capture up to (but not including) the :
delimiter
sed 's/(^[^:]*) :.*/1/' file
1.The Greater Company
5.United Taxi Service
31.The Cacao Company
40.Monster Inc.
55.Afala, Industrial Strength
60.The North Company
could you explain the command and how the back referencing works
– Rashad
Feb 16 at 1:28
@Rashad the pattern between(
and)
is captured, and then it is backreferenced as1
. See for example Regular Expression Reference: Capturing Groups and Backreferences
– steeldriver
Feb 16 at 1:34
You can omit the second colon:sed 's/(^[^:]*) .*/1/'
– dessert
Feb 16 at 7:44
add a comment |Â
up vote
1
down vote
accepted
If all of the lines begin with [0-9]*
there's no need to match it explicitly: just capture up to (but not including) the :
delimiter
sed 's/(^[^:]*) :.*/1/' file
1.The Greater Company
5.United Taxi Service
31.The Cacao Company
40.Monster Inc.
55.Afala, Industrial Strength
60.The North Company
could you explain the command and how the back referencing works
– Rashad
Feb 16 at 1:28
@Rashad the pattern between(
and)
is captured, and then it is backreferenced as1
. See for example Regular Expression Reference: Capturing Groups and Backreferences
– steeldriver
Feb 16 at 1:34
You can omit the second colon:sed 's/(^[^:]*) .*/1/'
– dessert
Feb 16 at 7:44
add a comment |Â
up vote
1
down vote
accepted
up vote
1
down vote
accepted
If all of the lines begin with [0-9]*
there's no need to match it explicitly: just capture up to (but not including) the :
delimiter
sed 's/(^[^:]*) :.*/1/' file
1.The Greater Company
5.United Taxi Service
31.The Cacao Company
40.Monster Inc.
55.Afala, Industrial Strength
60.The North Company
If all of the lines begin with [0-9]*
there's no need to match it explicitly: just capture up to (but not including) the :
delimiter
sed 's/(^[^:]*) :.*/1/' file
1.The Greater Company
5.United Taxi Service
31.The Cacao Company
40.Monster Inc.
55.Afala, Industrial Strength
60.The North Company
answered Feb 16 at 1:19
steeldriver
31.5k34978
31.5k34978
could you explain the command and how the back referencing works
– Rashad
Feb 16 at 1:28
@Rashad the pattern between(
and)
is captured, and then it is backreferenced as1
. See for example Regular Expression Reference: Capturing Groups and Backreferences
– steeldriver
Feb 16 at 1:34
You can omit the second colon:sed 's/(^[^:]*) .*/1/'
– dessert
Feb 16 at 7:44
add a comment |Â
could you explain the command and how the back referencing works
– Rashad
Feb 16 at 1:28
@Rashad the pattern between(
and)
is captured, and then it is backreferenced as1
. See for example Regular Expression Reference: Capturing Groups and Backreferences
– steeldriver
Feb 16 at 1:34
You can omit the second colon:sed 's/(^[^:]*) .*/1/'
– dessert
Feb 16 at 7:44
could you explain the command and how the back referencing works
– Rashad
Feb 16 at 1:28
could you explain the command and how the back referencing works
– Rashad
Feb 16 at 1:28
@Rashad the pattern between
(
and )
is captured, and then it is backreferenced as 1
. See for example Regular Expression Reference: Capturing Groups and Backreferences– steeldriver
Feb 16 at 1:34
@Rashad the pattern between
(
and )
is captured, and then it is backreferenced as 1
. See for example Regular Expression Reference: Capturing Groups and Backreferences– steeldriver
Feb 16 at 1:34
You can omit the second colon:
sed 's/(^[^:]*) .*/1/'
– dessert
Feb 16 at 7:44
You can omit the second colon:
sed 's/(^[^:]*) .*/1/'
– dessert
Feb 16 at 7:44
add a comment |Â
up vote
-2
down vote
grep -Pioe 's*d+.s*(w+[.,]? )+:' file
The question asks how to do this with back reference.   Does your answer use back reference?  Can you explain how?
– G-Man
Feb 16 at 4:22
add a comment |Â
up vote
-2
down vote
grep -Pioe 's*d+.s*(w+[.,]? )+:' file
The question asks how to do this with back reference.   Does your answer use back reference?  Can you explain how?
– G-Man
Feb 16 at 4:22
add a comment |Â
up vote
-2
down vote
up vote
-2
down vote
grep -Pioe 's*d+.s*(w+[.,]? )+:' file
grep -Pioe 's*d+.s*(w+[.,]? )+:' file
edited Feb 16 at 2:36
answered Feb 16 at 2:22
Budi
112
112
The question asks how to do this with back reference.   Does your answer use back reference?  Can you explain how?
– G-Man
Feb 16 at 4:22
add a comment |Â
The question asks how to do this with back reference.   Does your answer use back reference?  Can you explain how?
– G-Man
Feb 16 at 4:22
The question asks how to do this with back reference.   Does your answer use back reference?  Can you explain how?
– G-Man
Feb 16 at 4:22
The question asks how to do this with back reference.   Does your answer use back reference?  Can you explain how?
– G-Man
Feb 16 at 4:22
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f424507%2fi-want-to-extract-part-of-a-line-using-sed-and-backreference%23new-answer', 'question_page');
);
Post as a guest
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
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
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
1
It appears that you're removing the ranking, not "extracting the company number and rank"; can you clarify your goal vs your output?
– Jeff Schaller
Feb 16 at 1:13
I edited it. What i meant was just have the result being displayed like that. I can do this with sed by using sed 's/: .*//' filename.txt, but i want to know how to achieve this by using backreference.
– Rashad
Feb 16 at 1:17
When somebody asks “How do I do X?â€Â, we always ask “What have you tried?â€Â  Have you read any documentation?  What part of it did you not understand?  And is that a
:)
smiley face at the end of your question?  Or are you saying that the pattern that you want to match is[0-9]* .* :
, but you’re putting it in parentheses instead of quotes — this, when the input text contains parentheses (but not quotes).  That is very confusing. … (Cont’d)– G-Man
Feb 16 at 4:37
(Cont’d) …  What’s more confusing: you say that you want to match
[0-9]*  …
, but your data don’t have a space after the number. And you say that you want to match… :
, but your sample output shows that you don’t want to match the colon (:
), just everything up to it.– G-Man
Feb 16 at 4:37