简体   繁体   English

使用bash或python删除非字母数字字符

[英]Removing non-alphanumeric characters with bash or python

I have much files like these(please see the screenshot): 我有很多这样的文件(请看截图):

30.230201521829.jpg         
Mens-Sunglasses_L.180022111040.jpg   
progressive-sunglasses.180041285287.jpg
Atmosphere.222314222509.jpg  
Womens-Sunglasses-L.180023271958.jpg  
DAILY ESSENTIALS.211919012115.jpg
aviator-l.Sunglasses.240202216759.jpg 
aviator-l.Sunglasses.women.240202218530.jpg 

I want to raname them to the following: 我想将它们重命名为以下内​​容:

230201521829.jpg          
180022111040.jpg
180041285287.jpg
222314222509.jpg
172254027299.jpg
211919012115.jpg
240202216759.jpg 
240202218530.jpg

230201521829 is a timestamp ,180022111040 is a timestamp,180041285287 is a timestamp, etc. 230201521829是时间戳,180022111040是时间戳,180041285287是时间戳,依此类推。
Ensure that the final file name looks like "timestamp.jpg". 确保最终文件名看起来像“ timestamp.jpg”。

But I am not able to write the script more. 但是我无法编写更多脚本。
Sed(Bash) command or Python can be used to do it? 可以使用Sed(Bash)命令还是Python
Could you give me a example? 你能给我一个例子吗? Thanks. 谢谢。

在此处输入图片说明

Using command substitution for renaming the file. 使用命令替换重命名文件。 Following code will loop to the current directory's (unless path is modified) jpg files. 以下代码将循环到当前目录的jpg文件(除非修改了路径)。

Awk is used to filter out the penultimate and last column of file name which are separated by "." Awk用于过滤以“。”分隔的文件名倒数第二和最后一列。 .

for file in *.jpg
  do

    mv "$file" $(echo "$file" |awk -F'.' '{print $(NF-1)"." $NF}')

done

I use python 我用python

examp. 范例

import os
import sys
import glob

pth = "C:\Users\Test"
dir_show = os.listdir(pth)

for list_file in dir_show:
    if list_file.endswith(".JPG"):
        (shrname, exts) = os.path.splitext(list_file)
        path = os.path.join(pth, list_file)
        newname=os.path.join(pth,shrname[shrname.find(".")+1:len(shrname)]+".JPG")
        os.rename(path,newname)

Using perl rename one-liner: 使用perl重命名一线:

$ touch 30.230201521829.jpg Mens-Sunglasses_L.180022111040.jpg progressive-sunglasses.180041285287.jpg Atmosphere.222314222509.jpg Womens-Sunglasses-L.180023271958.jpg Womens-Eyeglasses-R.172254027299.jpg

$ ls -1
30.230201521829.jpg
Atmosphere.222314222509.jpg
Mens-Sunglasses_L.180022111040.jpg
progressive-sunglasses.180041285287.jpg
Womens-Eyeglasses-R.172254027299.jpg
Womens-Sunglasses-L.180023271958.jpg

$ prename -v 's/^[^.]*\.//' *.*.jpg
30.230201521829.jpg renamed as 230201521829.jpg
Atmosphere.222314222509.jpg renamed as 222314222509.jpg
Mens-Sunglasses_L.180022111040.jpg renamed as 180022111040.jpg
progressive-sunglasses.180041285287.jpg renamed as 180041285287.jpg
Womens-Eyeglasses-R.172254027299.jpg renamed as 172254027299.jpg
Womens-Sunglasses-L.180023271958.jpg renamed as 180023271958.jpg

You can use parameter expansion to strip off the extension, then remove all but the last . 您可以使用参数扩展剥离扩展名,然后除去最后一个. -delimited field from the remaining name. 剩余名称中的-分隔字段。 After than, you can reapply the extension. 之后,您可以重新应用扩展名。

for f in *; do
    ext=${f##*.}
    base=${f%.$ext}
    mv -- "$f" "${base##*.}.$ext"
done

The first line sets ext to the string following the last . 第一行将ext设置为last后面的字符串. . The second line sets base to the string that precedes the last . 第二行将base设置为last之前的字符串. (by removing the last . and whatever $ext was set to). (通过删除最后一个.以及$ext设置为的内容)。 The third line constructs a new file name by first removing everything up to, and including, the final . 第三行通过首先删除所有文件(包括final)来构造新的文件名. in base , then reapplying the extension to the result. base ,然后将扩展名重新应用于结果。

#!/bin/bash/
echo "test: "
echo "" > 30.230201521829.jpg         
echo "" > Mens-Sunglasses_L.180022111040.jpg   
echo "" > progressive-sunglasses.180041285287.jpg
echo "" > Atmosphere.222314222509.jpg  
echo "" > Womens-Sunglasses-L.180023271958.jpg  
echo "" > DAILY\ ESSENTIALS.211919012115.jpg
echo "" > aviator-l.Sunglasses.240202216759.jpg 
echo "" > aviator-l.Sunglasses.women.240202218530.jpg 
echo "before: "
ls -ltr
for f in *.jpg; do
       renamed=${f: -16}
       mv "${f}" "${renamed}"
done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM