Given an array of strings, I want IFS to parse each string by an underscore, join the first three separated values together using the same delimeter and output them to an empty list. For instance, if I have the array input=("L2Control_S3_L001_R1_001.fastq", "L2Control_S3_L001_R2_001.fastq") IFS would take the first string and separate it out by the underscore, so the output would be:
L2Control
S3
L001
R1
001.fastq
Afterward, it would take the first three separated values and join them together with an underscore: "L2Control_S3_L001". Lastly, this value would be appended onto a new array output=("L2Control_S3_L001") this process would continue until all values in the array are completed. I have tried the below implementation, but it seems to run infinitely.
#!/bin/bash
str=("L2Control_S3_L001_R1_001.fastq", "L2Control_S3_L001_R1_001.fastq")
IFS='_'
final=()
for (( c = 0; c = 2; c++ )); do
read -ra SEPA <<< "${str[$c]}"
final+=("${SEPA[0]}_${SEPA[1]}_${SEPA[2]}")
done
Can someone help me with this, please?
c < ${#str[@]},to separate array elements -- that comma is ending up as part of your data. (2) Note that you can set IFS local to a singlereadcommand.IFS=_ read -r -a arraynamewon't changeIFSfor any command other than thatread.read -aat all when you expect exactly three pieces.IFS=_ read -r first second thirdwill put the first piece in"$first", the second in"$second", everything after the second in"$third"and there you are.cat all?for a_str in "${str[@]}"; dowill assign each piece toa_strin turn.