Using Raku (formerly known as Perl_6)
If it's always paired reads, and they're always in the correct order:
~$ raku -e '.put for lines.rotor(2);' file
If it's always paired reads, but filenames are out-of-order:
~$ raku -e '.put for lines.sort.rotor(2);' file
Raku is a programming language in the Perl family. Similar to the excellent awk answer by @EdMorton, if you have to sort the filenames the resultant order will be alphabetic.
https://docs.raku.org/routine/lines
https://docs.raku.org/routine/rotor
https://raku.org
If sometimes files are missing, regardless of filename order:
~$ raku -ne 'BEGIN my %hash; \
%hash.append: .match(/^ (<-[_]>+) _ /).[0] => $_; \
END for %hash.sort { .values.put };' file
#OR
~$ raku -ne 'BEGIN my %hash; \
%hash.append: m/^ (<-[_]>+) _ /.[0] => $_; \
END for %hash.sort { .values.put };' file
This second approach is if the input data is less-than-pristine (missing filenames, etc.). Like Perl itself, Raku has an awk-like command-line mode invoked with the -ne ("non-autoprinting linewise") flags. Above, we BEGIN by declaring a %hash. In the body of this "one-liner" each line is .matched on (here .match is short for $_.match meaning to call the function on $_, the topic variable which holds the line text).
The .match (or m/ … /) routine/operator looks for ^ beginning-of-line followed by <-[_]>+ one-or-more of any-character-but _ underscore (i.e. a custom negative character class), followed by _ underscore itself. (FYI, a custom positive character class looks like so: <+[ … ]>). Parentheses capture the leading non-underscore text into $0 or more simply .[0].
Each line is thus deciphered into .[0] as key with $_ (the whole line) as value. The => is used to create a key/value pair. Hash data structures maintain unique keys, so every time the same key is encountered, the new value simply gets appended. At the END of reading lines, we sort the keys, and output the .values per key.
https://course.raku.org/essentials/associatives/hashes/
https://docs.raku.org/language/hashmap
https://raku.org
Sample Input:
A1_R1.fastq.gz
A1_R2.fastq.gz
A2_R1.fastq.gz
A2_R2.fastq.gz
A3_R1.fastq.gz
A3_R2.fastq.gz
Sample Output (either approach):
A1_R1.fastq.gz A1_R2.fastq.gz
A2_R1.fastq.gz A2_R2.fastq.gz
A3_R1.fastq.gz A3_R2.fastq.gz
R1andR2file for every prefix (A1,A2, etc.)? Thx.paste - - <yourfilewould suffice.A1_R1comes before or afterA1_R2on the output line for that pair?_R1and_R2files come together in the listing, one immediately following the other?