我需要帮助将连续编号放在我的LINUX文件名中间。

huangapple go评论58阅读模式
英文:

I need help to put a sequential number in the middle of my filenames in LINUX

问题

我有一个目录中的一些文件,我想将它们重命名,以便它们具有顺序。

原始文件名:

Customer_Branch_Record.20230303.1349.DAT
Customer_Branch_Record.20230303.1356.DAT
Customer_Branch_Record.20230303.1527.DAT

我想在文件名之间放置序列01、02、03等,输出如下:

Customer_Branch_Record.01.20230303.1349.DAT
Customer_Branch_Record.02.20230303.1356.DAT
Customer_Branch_Record.03.20230303.1527.DAT

谢谢!

我尝试使用bash进行重命名,但它只能重命名为新名称,而不使用原始详细信息。

#!/bin/bash

a=1
for i in Cust*; do
  new=$(printf "Customer_Document_Advices.%02d.txt" "$a") #04 pad to length of 2
  mv -i -- "$i" "$new"
  let a=a+1
done

输出:

Customer_Document_Advices.02.txt
Customer_Document_Advices.01.txt
Customer_Document_Advices.03.txt

请帮助以这样的方式,我可以在中间放置一个序列,同时保留文件名的原始详细信息。

英文:

I have some files in a directory and I want to rename them in such a way they will have sequence.

Original Filenames:

Customer_Branch_Record.20230303.1349.DAT
Customer_Branch_Record.20230303.1356.DAT
Customer_Branch_Record.20230303.1527.DAT

I want to put a sequence 01 02 03 and so on in between the filenames and output below

Customer_Branch_Record.01.20230303.1349.DAT
Customer_Branch_Record.02.20230303.1356.DAT
Customer_Branch_Record.03.20230303.1527.DAT

Thanks!

I tried bash to rename but it can only rename into a new name and does not use the original details

#!/bin/bash

a=1
for i in Cust*; do
  new=$(printf "Customer_Document_Advices.%02d.txt" "$a") #04 pad to length of 2
  mv -i -- "$i" "$new"
  let a=a+1
done

OUTPUT:

Customer_Document_Advices.02.txt
Customer_Document_Advices.01.txt
Customer_Document_Advices.03.txt

please help in such a way that I can place a sequence in the middle and still retain the original details of the filename

答案1

得分: 0

使用 Perl 的 rename

$ rename 's/(\.\d{8})/"." . sprintf("%0.2d", ++$::c) . $1/e' ./Customer*

ls -1

Customer_Branch_Record.01.20230303.1349.DAT
Customer_Branch_Record.02.20230303.1356.DAT
Customer_Branch_Record.03.20230303.1527.DAT
英文:

Using Perl's rename:

$ rename 's/(\.\d{8})/"." . sprintf("%0.2d", ++$::c) . $1/e' ./Customer*

ls -1

Customer_Branch_Record.01.20230303.1349.DAT
Customer_Branch_Record.02.20230303.1356.DAT
Customer_Branch_Record.03.20230303.1527.DAT

答案2

得分: 0

您可以尝试:

a=1
while read -r line; do
    echo mv $line $(sed -E "/(\.[^.]*){4,}/ ! s/[^.]*/&.$a/" <<< $line)
    let a+=1
done < <(find /path/to/directory -type f -name 'Cust*' -printf '%f\n')

如果输出如预期,去掉 echo 以保存更改。

英文:

You can try;

a=1
while read -r line; do
    echo mv $line $(sed -E &quot;/(\.[^.]*){4,}/ ! s/[^.]*/&amp;.$a/&quot; &lt;&lt;&lt; $line)
    let a+=1
done &lt; &lt;(find /path/to/directory -type f -name &#39;Cust*&#39; -printf &#39;%f\n&#39;)

If the output is as expected, remove the echo to persist the changes

huangapple
  • 本文由 发表于 2023年3月3日 18:33:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75625950.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定