Bash Tricks: Split / Cut a String with Multi Character Delimiters

Its simple enough to split a string when it has single character delimiters using the cut command. However cut doesn’t support multi-character delimiters. Here’s a sample script to demonstrate how to split strings with multi-character delimiters.

#Inputs to the script, the delimiter, and the string itself
D="<>"   #Multi Character Delimiter
string="abcd<>efgh<>ijkl<>mn op<>qr st<>uv wx<>yz" #String with delimiters

#Split the String into Substrings
sList=($(echo $string | sed -e 's/'"$D"'/\n/g' | while read line; do echo $line | sed 's/[\t ]/'"$D"'/g'; done))
for (( i = 0; i < ${#sList[@]}; i++ )); do
  sList[i]=$(echo ${sList[i]} | sed 's/'"$D"'/ /')

#Output the Split String
echo No of SubStrings - ${#sList[@]}
for (( i = 0; i < ${#sList[@]}; i++ )); do
  echo ${sList[i]}

In the above script, the string is being split and then stored in the sList array. You can access the individual substrings using ${sList[0]}, ${sList[1]}, ${sList[2]} etc. The output for the above script is:

@$ sh 
No of SubStrings - 7
mn op
qr st
uv wx

For those who are wondering how to cut using the cut command. Here’s an example

echo "a|b|c|d|e" |  cut -d '|' -f 1
echo "a|b|c|d|e" |  cut -d '|' -f 2
echo "a|b|c|d|e" |  cut -d '|' -f 3


  1. A better solution.
    echo “abcdefghijklmn opqr stuv wxyz”|awk -F ” ‘{print ” item1:” $1 ” item2:” $2 ” item3:” $3 ” item4:” $4 ” item5:” $5 ” item6:” $6 ” item7:” $7 ” item8:” $8}’

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.