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

The name of the pictureThe name of the pictureThe name of the pictureClash 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]* .* :).







share|improve this question


















  • 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














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]* .* :).







share|improve this question


















  • 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












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]* .* :).







share|improve this question














 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]* .* :).









share|improve this question













share|improve this question




share|improve this question








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












  • 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










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





share|improve this answer




















  • 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










  • You can omit the second colon: sed 's/(^[^:]*) .*/1/'
    – dessert
    Feb 16 at 7:44


















up vote
-2
down vote













grep -Pioe 's*d+.s*(w+[.,]? )+:' file





share|improve this answer






















  • 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










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',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















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






























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





share|improve this answer




















  • 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










  • You can omit the second colon: sed 's/(^[^:]*) .*/1/'
    – dessert
    Feb 16 at 7:44















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





share|improve this answer




















  • 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










  • You can omit the second colon: sed 's/(^[^:]*) .*/1/'
    – dessert
    Feb 16 at 7:44













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





share|improve this answer












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






share|improve this answer












share|improve this answer



share|improve this answer










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 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

















  • 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










  • 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













up vote
-2
down vote













grep -Pioe 's*d+.s*(w+[.,]? )+:' file





share|improve this answer






















  • 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














up vote
-2
down vote













grep -Pioe 's*d+.s*(w+[.,]? )+:' file





share|improve this answer






















  • 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












up vote
-2
down vote










up vote
-2
down vote









grep -Pioe 's*d+.s*(w+[.,]? )+:' file





share|improve this answer














grep -Pioe 's*d+.s*(w+[.,]? )+:' file






share|improve this answer














share|improve this answer



share|improve this answer








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
















  • 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












 

draft saved


draft discarded


























 


draft saved


draft discarded














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













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay