In the bash
below I am trying to loop through all the R1.gz
in a directory (always 1), store them in ARRAY
, and cut them before the second _
. That is being done but I can't seem to print then one a single line seperated by a space. Is the below the best way or is there a better solution? Thank you :).
# create an array with all the fastq.gz in directory
ARRAY=(/path/to/*R1*.gz) ## grab only the R1 and store in ARRAY
# iterate through array
for f in "${array[@]}"; do ## loop through ARRAY
bname=$(basename "$f") ## strip path from ARRAY
s=($(echo $bname|cut -d_ -f1,2)) # remove after second underscore
echo ${s[@]} --- also tried "${s//$'\n'/ }" ## print on single line
done ## end loop
files in /path/to
00-0000-xxx_S7_R1_000.gz
00-0000-xxx_S7_R2_000.gz
00-0001-xxx_S1_R1_000.gz
00-0001-xxx_S1_R2_000.gz
00-0002-xxx_S2_R1_000.gz
00-0000-xxx_S2_R2_000.gz
current
00-0000-xxx_S7
00-0001-xxx_S1
00-0002-xxx_S2
desired
00-0000-xxx_S7 00-0001-xxx_S1 00-0002-xxx_S2