Search code examples
linuxbashunixcut

Split a string on a word in Bash


I wish to be able to split a string on a word. Essentially a multi-character delimiter. For example, I have a string:

test-server-domain-name.com

I wish to keep everything before 'domain' so the output would be:

test-server-

Note: I cannot cut on the '-'. I have to be able to cut before the term 'domain' as the string's format will vary but 'domain' will always be present and I will always want to capture the elements before 'domain'.

Is this possible in bash?


Solution

  • Use awk:

    echo test-server-domain-name.com | awk -F 'domain' '{print $1}'