I have a couple of files, which look like this:
1_150901_AC7GLHANXX_P2258_101_1.fastq.gz
1_150901_AC7GLHANXX_P2258_101_2.fastq.gz
2_150901_AC7GLHANXX_P2258_101_1.fastq.gz
2_150901_AC7GLHANXX_P2258_101_2.fastq.gz
... i.e., there are two files that start with 1_
and end in either _1.fastq.gz
or _2.fastq.gz
, and the same for two files that start with 2_
. What I want to do is to cat
the two files ending in _1.fastq.gz
, like this:
cat 1_150901_AC7GLHANXX_P2258_101_1.fastq.gz \
2_150901_AC7GLHANXX_P2258_101_1.fastq.gz \
> 150901_AC7GLHANXX_P2258_101_1.fastq.gz
... so that they are merged and have their prefix removed. I have a lot more files in a lot more folders than this, so I want to automate it. I tried the following code, to no avail:
for f in *_*_1.fastq.gz
do
cat $f "${f/^1_/2_}" > "${f/^1_/}"
done
I don't think I know this replacement-method well enough, but it's what I have used in the past for less complicated filenames (when they only have different sufficex, and no prefix). I think that the ^
at the beginning signifies start of the filename, but it doesn't seem to work like I want it to, so obviously I'm doing something wrong. I tried doing some troubleshooting:
for f in *_*_1.fastq.gz
do
echo "${f/^1_/}"
done
... gives me ...
1_150901_AC7GLHANXX_P2258_101_1.fastq.gz
2_150901_AC7GLHANXX_P2258_101_1.fastq.gz
... which is not what I thought it would be. Does anybody know how I could do this?
[Edit, clarify non-duplicated question]
This question is different from my previous question in that I also have a prefix for the filenames, and that prefix also exists in the middle of the filename. The other question had a simpler case, where only a suffix was what needed to be renamed.