Loop through directory and print on line

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

One thing that jumps out is: ARRAY != array

1 Like

It kind of depends what you want to do with the list. You can certain cut down the effort in your code by not starting lots of processes, but using Variable Substitution (look in the bash manual pages), perhaps like this:-

for file in *
do
   output="$output ${file%_*_*}"
done

echo $output

This is not unique though. Is that a problem? We can do sneaky things with what bash calls an Associative Array (or a Hash) like this:-

declare -A output
for file in *
do
   key="${file%_*_*}"
   output["${key}"]=true
done

echo "${!output[@]}"

Okay, so that is not sorted. is that a problem?

I hope that these tricks help.

Robin

2 Likes

Thank you very much :slight_smile: