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

时间:2016-08-24 10:08:12

标签: python bash sed

我有很多这样的文件(请参见截图):

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 

我想将它们命名为以下内​​容:

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

230201521829是时间戳,180022111040是时间戳,180041285287是时间戳等。
确保最终文件名称为“timestamp.jpg”。

但我无法更多地编写脚本 可以使用 Sed(Bash)命令或 Python 来执行此操作吗? 你能举个例子吗?谢谢。

enter image description here

3 个答案:

答案 0 :(得分:1)

我使用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)

答案 1 :(得分:0)

使用perl rename单行:

$ 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

答案 2 :(得分:0)

然后,您可以使用参数扩展来剥离扩展名 从剩余名称中删除除最后SEVERE: Exception sending context initialized event to listener instance of class org.activiti.rest.servlet.WebConfigurer org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'demoDataConfiguration': Invocation of init method failed; nested exception is java.lang.AbstractMethodError: org.activiti.bpmn.converter.child.BaseChildElementParser.parseChildElement(Ljavax/xml/stream/XMLStreamReader;Lorg/activiti/bpmn/model/BaseElement;Lorg/activiti/bpmn/model/BpmnModel;)V at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:136) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:408) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1558) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480) at org.activiti.rest.servlet.WebConfigurer.contextInitialized(WebConfigurer.java:45) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4961) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5455) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901) at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877) at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:634) at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1230) at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1876) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) at java.util.concurrent.FutureTask.run(FutureTask.java:262) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.java:745) Caused by: java.lang.AbstractMethodError: org.activiti.bpmn.converter.child.BaseChildElementParser.parseChildElement(Ljavax/xml/stream/XMLStreamReader;Lorg/activiti/bpmn/model/BaseElement;Lorg/activiti/bpmn/model/BpmnModel;)V at org.activiti.bpmn.converter.util.BpmnXMLUtil.parseChildElements(BpmnXMLUtil.java:126) at org.activiti.bpmn.converter.BaseBpmnXMLConverter.parseChildElements(BaseBpmnXMLConverter.java:253) at org.activiti.bpmn.converter.BaseBpmnXMLConverter.parseChildElements(BaseBpmnXMLConverter.java:243) at org.activiti.bpmn.converter.StartEventXMLConverter.convertXMLToElement(StartEventXMLConverter.java:55) at org.activiti.bpmn.converter.BaseBpmnXMLConverter.convertToBpmnModel(BaseBpmnXMLConverter.java:91) at org.activiti.bpmn.converter.BpmnXMLConverter.convertToBpmnModel(BpmnXMLConverter.java:426) at org.activiti.bpmn.converter.BpmnXMLConverter.convertToBpmnModel(BpmnXMLConverter.java:284) at org.activiti.engine.impl.bpmn.parser.BpmnParse.execute(BpmnParse.java:187) at org.activiti.engine.impl.bpmn.deployer.BpmnDeployer.deploy(BpmnDeployer.java:136) at org.activiti.engine.impl.persistence.deploy.DeploymentManager.deploy(DeploymentManager.java:58) at org.activiti.engine.impl.cmd.DeployCmd.execute(DeployCmd.java:106) - 分隔字段以外的所有字段。之后,您可以重新申请扩展名。

.

第一行将for f in *; do ext=${f##*.} base=${f%.$ext} mv -- "$f" "${base##*.}.$ext" done 设置为最后一个ext后面的字符串。第二行将.设置为前一个base之前的字符串(通过删除最后.以及设置为.的任何内容)。第三行构建新文件名,首先删除$ext中的所有内容,包括最终.,然后将扩展名重新应用到结果中。