I have a string like
data1_data2_data3_data4@data5,data6
It happens that, sometimes, data5 contains underscores, which happens to be the field separator. Ugly, I know.
I want to read this data pieces with something like:
IFS="_@," read d1 d2 d3 d4 d5 d6 <<< "$input"
The problem arrives when data5 contains an underscore. To work around this issue. I want to substitute the first three underscores with commas (and the @ too). The easies way I found so far is with sed:
sed 's/_/,/; s/_/,/; s/_/,/; s/@/,/' <<< "$input"
But repeating three times the same substitution seems quite inefficient. What happens if I need to repeat it 5000 times?
Is there any way to tell sed to repeat a substitution a certain amount of times?
To be complete, sample input:
input="data1_data2_data3_data4@d_a_t_a_5,data6"
IFS="," read d1 d2 d3 d4 d5 d6 <<< "$input"
Expected output:
d1=="data1"
d2=="data2"
d3=="data3"
d4=="data4"
d5=="d_a_t_a_5"
d6=="data6"