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

  • 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 '18 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 '18 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 '18 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 '18 at 4:37
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.