Skip to main content
deleted 15 characters in body
Source Link
jesse_b
  • 41.6k
  • 14
  • 109
  • 163

NotSort of a native way to do it but with sedusing both parameter expansion and readarray:readarray

string='This is some sep.string' mapfile -t array < <(echo 'This is some sep.string' | sedecho $'s"${string//sep./\\\n/g'$'\n'}" ) 

sed${string//sep./$'\n'} will change all- Will replace occurrences of sep. intowith a newline which when combined with . readarraymapfile/mapfilereadarray will cause everything between occurrences of sep. to be added tocreate an array with each line in it's own element in array.

Not a native way to do it but with sed and readarray:

mapfile -t array < <(echo 'This is some sep.string' | sed $'s/sep./\\\n/g') 

sed will change all occurrences of sep. into a newline which when combined with readarray/mapfile will cause everything between occurrences of sep. to be added to it's own element in array.

Sort of a native way to do it using both parameter expansion and readarray

string='This is some sep.string' mapfile -t array < <( echo "${string//sep./$'\n'}" ) 

${string//sep./$'\n'} - Will replace occurrences of sep. with a newline. mapfile/readarray will create an array with each line in it's own element.

Source Link
jesse_b
  • 41.6k
  • 14
  • 109
  • 163

Not a native way to do it but with sed and readarray:

mapfile -t array < <(echo 'This is some sep.string' | sed $'s/sep./\\\n/g') 

sed will change all occurrences of sep. into a newline which when combined with readarray/mapfile will cause everything between occurrences of sep. to be added to it's own element in array.