1

I would like to split the string with delimiter. variable my_string contains Hello_This_Is_My_string The output will be only

Below is my code :

result = $(echo $my_string |" cut -d '_' -f2")

However, I am getting <Is> instead of <This_Is_My_string>

2 Answers 2

10

I have found an answer:

result=$(echo $my_string | cut -d "_" -f 2-)

Test with:

echo aa_bb_cc | cut -d "_" -f 2-
Sign up to request clarification or add additional context in comments.

4 Comments

If it works and is the answer, mark it as answer. Others may find it useful.
I cant mark the answer as there is a prompt said "You can accept your own answer in 2 days"
I do not understand your quotes. Use result=$(echo $my_string | cut -d "_" -f 2-).
@WalterA to cut the string with a delimiter "_"
2

Avoid calling external commands, do it with shell-internal 'Parameter Expansion', which has quite powerfull options

my_string='Hello_This_Is_My_string'
echo "result = ${my_string#*_}"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.