我正在嘗試重命名一些檔案,并且對正則運算式非常陌生。我知道如何長期這樣做,但我正在嘗試一些代碼高爾夫來縮短它。
我的檔案:
abc4800_12_S200_R1_001.fastq.gz
我的目標:
abc4800_12_R1.fastq.gz
現在我有一個重命名它的兩步程序:
rename 's/_S[0-9] //g' *gz
rename 's/_001//g' *gz
但是我試圖將其縮短為一行以一次性清理它。
我試圖使用正則運算式跳過中間的部分,但如果這在這個函式中實際上是可能的,那就不要了。
rename 's/_S[0-9] _*?_001//g' *gz
謝謝你的幫助
uj5u.com熱心網友回復:
使用捕獲組保留要替換的段的中間部分。
rename 's/_S\d _(.*)_001/_$1/' *gz
uj5u.com熱心網友回復:
使用您顯示的示例,請嘗試以下rename命令。我在-n這里使用選項,這是命令的試運行,一旦您對輸出感到滿意(例如,如果我們運行實際代碼,檔案將如何重命名)然后-n從以下rename代碼中洗掉選項。
rename -n 's/(^[^_]*_[^_]*)_[^_]*(_[^_]*)[^.]*(\..*$)/$1$2$3/' *.gz
輸出如下:
rename(abc4800_12_S200_R1_001.fastq.gz, abc4800_12_R1.fastq.gz)
說明:為以上添加詳細說明。
(^[^_]*_[^_]*) ##Creating 1st capturing group which capture everything from starting to just before 2nd occurrence of _ here.
_[^_]* ##Matching(without capturing group) _ then just before next occurrence of _ here.
(_[^_]*) ##Creating 2nd capturing group here which matches _ followed by before next occurrence of _ here.
[^.]* ##Matching everything just before dot comes(not capturing here).
(\..*$) ##Creating 3rd capturing group which has dot till end of line in it.
uj5u.com熱心網友回復:
您正試圖用空替換字串中的兩個部分。使用交替運算子,它將匹配左側或右側;用相同的替換字串替換任何匹配項(即沒有):
rename 's/_S[0-9] |_001//g' *gz
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/351513.html
