I would like to match the \d\dQ\d
pattern or \dQ\d\d
pattern and reformat it in front of the file name. Using OSX Terminal or ruby, what would be the best method to do this? It is a little more complicated because it is pattern 1 or pattern 2.
In the first file, pattern "3Q17" for example is a date that means the 3rd quarter of year 2017. I need to add 2017Q3 in front of the file name. However, I have another pattern "05Q1" which means the 1st quarter of 2005, so I would need to add 2005Q1.
Files:
ABC-EDFGH-JLG-Sample-3Q17-fIS.pdf
^^^^
2Q13 ABC MF PM Example_fIS.pdf
^^^^
03Q1_FIS.pdf
^^^^
05Q1 ABC_IS.pdf
^^^^
Files renamed:
2017Q3_ABC-EDFGH-JLG-Sample-3Q17-fIS.pdf
2013Q2_2Q13 ABC MF PM Example_fIS.pdf
2003Q1_03Q1_FIS.pdf
2005Q1_05Q1 ABC_IS.pdf
This question helps with a single pattern, but not 2 patterns.
mac os x terminal batch rename
My ruby code works fine:
Dir.glob("*.pdf").each do |orig_name|
new_name = orig_name.gsub(/^.*?(?:(\d\d)Q(\d)|(\d)Q(\d\d))/, '20\1\4Q\2\3_\0')
File.rename(orig_name, new_name)
end
However, my rename script (using brew install rename) in the terminal provides the wrong results.
rename -n -e 's/^.*?(?:(\d\d)Q(\d)|(\d)Q(\d\d))/'20\1\4Q\2\3_\0'/' *.pdf
'03Q1_FIS.pdf' would be renamed to '2014Q23_0_FIS.pdf'
'03Q1_vIS.pdf' would be renamed to '2014Q23_0_vIS.pdf'
'05Q1 ABC_IS.pdf' would be renamed to '2014Q23_0 ABC_IS.pdf'
'2Q13 ABC MF PM Example_fIS.pdf' would be renamed to '2014Q23_0 ABC MF PM Example_fIS.pdf'