Search code examples
bashgrepcat

How to get only one part of the string without the end in bash


I want to get only one part of the string new-profile-input, the part that I need is: "new-profile" without the "-input".

I tried like this:

cat automatization_test.sh |  grep -oh "\new-profile-input\w*" | grep -o "\-input\w*"

But, I get output:

-input

But, I need the first part not the last part of the string. Please note that the "new-profile" will always change, so that is why I have to focus on removing "-input" instead of getting only "new-profile".


Solution

  • Using sed, you can exclude everything after the last - slash where -input would be in your example string.

    $ sed 's/\(.*\)-.*/\1/' automatization_test.sh
    new-profile