I am trying to rename some files and am pretty new to regular expression. I know how to do this the long way, but am trying some code golf to shorten it up.
my file:
abc4800_12_S200_R1_001.fastq.gz
my goal:
abc4800_12_R1.fastq.gz
right now I have a two step process for renaming it:
rename 's/_S[0-9] //g' *gz
rename 's/_001//g' *gz
But I was trying to shorten this into one single line to clean it up in one go.
I was trying to use regular expression to skip over the parts in between, but dont if that is actually a possibility in this function.
rename 's/_S[0-9] _*?_001//g' *gz
Thanks for any help
CodePudding user response:
Use a capture group to preserve the middle part of the segment you're replacing.
rename 's/_S\d _(.*)_001/_$1/' *gz
CodePudding user response:
You are trying to replace two parts in the string with nothing. Use the alternation operator, it will match the left or the right side; replacing any match with the same replacement string (i.e. nothing):
rename 's/_S[0-9] |_001//g' *gz