Split string using 2 different delimiters in Bash

geekyjazzy picture geekyjazzy · Aug 6, 2014 · Viewed 7.1k times · Source

I am trying to split a string in BASH based on 2 delimiters - Space and the \. This is the string:-

var_result="Pass results_ADV__001__FUNC__IND\ADV__001__FUNC__IND_08_06_14_10_04_34.tslog"

I want it to split in 3 parts as follows:-

part_1=Pass
part_2=results_ADV__001__FUNC__IND
part_3=ADV__001__FUNC__IND_08_06_14_10_04_34.tslog

I have tried using IFS and it splits the first one well. But the second split some how removes the "\" and sticks the entire part and I get the split as :-

test_res= Pass
log_file_info=results_ADV__001__FUNC__INDADV__001__FUNC__IND_08_06_14_10_04_34.tslog

The IFS I used is as follows:-

echo "$var_result"
IFS=' ' read -a array_1 <<< "$var_result"
echo "test_res=${array_1[0]}, log_file_info=${array_1[1]}"

Thanks in advance.

Answer

Mark Setchell picture Mark Setchell · Aug 6, 2014

I think you need this:

IFS=' |\\' read -ra array_1 <<< "$var_result"