0

I have several recordings with a file-name schema like this

%year%month%day-%hour%minute%second__%phonenumber.amr

e.g.:
20190212-112007__+313206601234.amr

I want to swap words to obtain:

%phonenumber_%day-%month-%year_%hour-%minute-%second.amr

e.g.:
+313206601234__12-02-2019_11-20-07.amr

I know I can use sed utility but its syntax is like an archaic language, plus I'm confused about the use of both [0-9] and . for substitution.

  • The archaic language is regular expressions. However sed if for file content and streams. rename (the Larry Wall version), is sed for file-names. – ctrl-alt-delor Apr 7 at 10:14
0

It's all about using captured groups of digits, and putting groups at desired places to get the required name:

sed -E 's/^([[:digit:]]{4})([[:digit:]]{2})([[:digit:]]{2})-([[:digit:]]{2})([[:digit:]]{2})([[:digit:]]{2})__(\+[[:digit:]]+)/\7__\3-\2-\1_\4-\5-\6/'
  • -E enables ERE (Extended Regular Expressions)

  • [[:digit:]] matches a locale-aware digit

  • {N} matches N number of preceding token e.g. [[:digit:]]{2} matches 2 digits; this can also be a range e.g. [[:digit:]]{2,4} matches 2 to 4 digits

Example:

% sed -E 's/^([[:digit:]]{4})([[:digit:]]{2})([[:digit:]]{2})-([[:digit:]]{2})([[:digit:]]{2})([[:digit:]]{2})__(\+[[:digit:]]+)/\7__\3-\2-\1_\4-\5-\6/' <<<'20190212-112007__+313206601234.amr'
+313206601234__12-02-2019_11-20-07.amr
  • @mattia.b89: With prename (Perl's standalone rename command): prename -n 's/^([[:digit:]]{4})([[:digit:]]{2})([[:digit:]]{2})-([[:digit:]]{2})([[:digit:]]{2})([[:digit:]]{2})__(\+[[:digit:]]+)/$7__$3-$2-$1_$4-$5-$6/' *.amr. If everything looks fine, remove -n. – Cyrus Apr 7 at 11:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.