I'm attempting to take the last word or phrase using grep for a specific pattern. In this example, it would be the from the last comma to the end of the line:

Blah,3,33,56,5,Foo 30,,,,,,,3,Great Value

And so the wanted output for that line would be "Great Value". All the lines are different lengths as well, but always have a single comma preceding the last words.

Basically, I would like to simply output from the last comma to the end of the line. Thank you!

share|improve this question
1  
Have you considered using awk -F, '{print $NF}'? – steeldriver 3 hours ago
    
Getting last item on the line is easy task, but how should that line be identified ? i mean, how can a command or script look at the line, and say "yep, that's the one" ? what's the pattern that identifies that line ? – Serg 2 hours ago

Here:

grep -o '[^,]\+$'
  • [^,]\+ matches one or more characters that are not , at the end of the line ($)

  • -o prints only the matched portion

Example:

% grep -o '[^,]\+$' <<<'Blah,3,33,56,5,Foo 30,,,,,,,3,Great Value'
Great Value
share|improve this answer
    
Worked like a charm! I was trying to use a combination of "." and other expressions before but this works a lot better, much appreciated. – reversebottle 3 hours ago
    
@reversebottle No problem :) – heemayl 3 hours ago

Always like to see an awk solution so here it is (upvoted the proper solution!):

% awk -F, '{print $NF}'  <<<'Blah,3,33,56,5,Foo 30,,,,,,,3,Great Value'
Great Value
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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