Perlnika
Perlnika

Reputation: 5066

substring from bash variable starting from character position

simple question, but my sed command is not working :(

echo "some_class" | sed -e 's/.+\_\(.+\)/\1/'

I want everything that happens to be after _ character. Please, how can I fix my code to work? Thank you.

In this case, "class" is the right answer.

Upvotes: 2

Views: 297

Answers (2)

Kent
Kent

Reputation: 195199

this sed line works for your example:

sed 's/.*_//'

with your example:

kent$  echo "some_class"|sed 's/.*_//'
class

Upvotes: 3

chepner
chepner

Reputation: 531858

You don't need sed; you can use parameter expansion to return the substring beginning with the first _:

foo=some_class
echo "${foo#*_}"

Upvotes: 3

Related Questions