Suppose there is в list of поля with the following pattern:
ABC2011DEFG.txt
What command should I использовал to remove characters before or after the number in between while keeping the file расширение?
Please заметил that the number of redundant characters is not fixed.
Используя rename
:
rename -n 's/[^0-9]*//' *.txt # removes the first sequence of non-digits
rename -n 's/[^0-9]*\./\./' *.txt # removes the first sequence of non-digits followed by a dot
% rename -n 's/[^0-9]*//' *.txt
rename(ABC2011DEFG.txt, 2011DEFG.txt)
rename(HIJ2012KLMN.txt, 2012KLMN.txt)
rename(OPQ2013RSTU.txt, 2013RSTU.txt)
% rename -n 's/[^0-9]*\./\./' *.txt
rename(ABC2011DEFG.txt, ABC2011.txt)
rename(HIJ2012KLMN.txt, HIJ2012.txt)
rename(OPQ2013RSTU.txt, OPQ2013.txt)
, Если файлы будут переименованными как ожидалось, удалите -n
опция.
Попробуйте это:
rename 's/[^0-9]*([0-9]*)[^0-9]*(\..*)/$1$2/' *
[^0-9]*
средства что-либо кроме цифр, любое количество раз [0-9]*
означает только цифры, любое количество раз (\..*)
означает точку (\.
), тогда что-либо (.*
) - расширение $1
является содержанием того, что соответствовало первым круглым скобкам (цифры) $2
содержание того, что соответствовало вторым круглым скобкам (расширение) Поэтому, если Вы имеете:
2012xyz.txt ABC2011DEFG.txt zyx2013.txt
Вы доберетесь:
2011.txt 2012.txt 2013.txt