在linux中使用regex重命名文件

时间:2022-06-19 10:32:56

Hi I need to change these filenames in a folder:

嗨,我需要在文件夹中更改这些文件名:

dingo__AAA311P02_2845__L7_ACTTGA_L007_R1_001.fastq.gz
lala_bros__AAA000M11_3289__L2_ACTGAT_L002_R2_001.fastq.gz
nice__AAA827M10_2860__L7_ACTGAT_L007_R2_001.fastq.gz

I need to retain the AAA****** (eg. AAA255P02) and also keep the R1 or R2 and the extension fastq.gz

我需要保留AAA ******(例如AAA255P02)并保留R1或R2和扩展名fastq.gz

So ideally i would like to get these:
AAA311P02_R1.fastq.gz AAA000M11_R2.fastq.gz AAA827M10_R2.fastq.gz

理想情况下,我想得到这些:AAA311P02_R1.fastq.gz AAA000M11_R2.fastq.gz AAA827M10_R2.fastq.gz

I tried using rename 's/^.*\(AAA[1-9][1-9][1-9][A-Z][1-9][1-9]\).*$/\$1/g' *.fastq.gz

我尝试使用重命名的/^.* \(AAA [1-9] [1-9] [1-9] [AZ] [1-9] [1-9] \)。* $ / \ $ 1 / g'* .fastq.gz

1 个解决方案

#1


\(...\) searches for parenthesis but does not capture its contents.

\(... \)搜索括号但不捕获其内容。

I see three blocks to get captured, so I'd propose the following:

我看到有三个块被捕获,所以我建议如下:

rename 's/^.*(AAA.{6}).*(_R.).*(\.fastq.gz)/$1$2$3/' *.fastq.gz

1st block: AAA followed by six characters.

第一块:AAA后跟六个字符。

2nd block: _R followed by a single character.

第二块:_R后跟一个字符。

3rd block: the extension .fastq.gz

第3块:扩展名.fastq.gz

#1


\(...\) searches for parenthesis but does not capture its contents.

\(... \)搜索括号但不捕获其内容。

I see three blocks to get captured, so I'd propose the following:

我看到有三个块被捕获,所以我建议如下:

rename 's/^.*(AAA.{6}).*(_R.).*(\.fastq.gz)/$1$2$3/' *.fastq.gz

1st block: AAA followed by six characters.

第一块:AAA后跟六个字符。

2nd block: _R followed by a single character.

第二块:_R后跟一个字符。

3rd block: the extension .fastq.gz

第3块:扩展名.fastq.gz