在扩展名之前从Unix文件名中删除最后n个字符 [英] Removing last n characters from Unix Filename before the extension

查看:86
本文介绍了在扩展名之前从Unix文件名中删除最后n个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Unix目录中有一堆文件:

I have a bunch of files in Unix Directory :

  test_XXXXX.txt  
  best_YYY.txt   
  nest_ZZZZZZZZZ.txt

我需要将这些文件重命名为

I need to rename these files as

  test.txt   
  best.txt   
  nest.txt

我正在AIX上使用Ksh.请让我知道如何使用单个"命令来完成上述操作.

I am using Ksh on AIX .Please let me know how i can accomplish the above using a Single command .

谢谢

推荐答案

如果这是一次(或者不是很常见),我将使用

If this is a one time (or not very often) occasion, I would create a script with

$ ls > rename.sh
$ vi rename.sh
:%s/\(.*\)/mv \1 \1/
(edit manually to remove all the XXXXX from the second file names)
:x
$ source rename.sh

如果这种需求经常发生,我将需要更多了解XXXXX,YYY和ZZZZZZZZZZZ是什么.

If this need occurs frequently, I would need more insight into what XXXXX, YYY, and ZZZZZZZZZZZ are.

附录

根据自己的喜好进行修改:

Modify this to your liking:

ls | sed "{s/\(.*\)\(............\)\.txt$/mv \1\2.txt \1.txt/}" | sh

它通过省略.txt之前的12个字符并将生成的mv命令传递到外壳程序来转换文件名.

It transforms filenames by omitting 12 characters before .txt and passing the resulting mv command to a shell.

当心:如果文件名不匹配,它将执行文件名—而不执行mv命令.我省略了只选择匹配文件名的方法.

Beware: If there are non-matching filenames, it executes the filename—and not a mv command. I omitted a way to select only matching filenames.

这篇关于在扩展名之前从Unix文件名中删除最后n个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆