本文介绍了尝试在Mac OS X上使用sed重命名文件时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组从0001开始按顺序编号的文件.我要完成的工作是重命名更改文件名,同时保持某些片段(例如nw,w)完整,并从此开始编号0000.

I have a set of files that are sequentially numbered from 0001. What I'm trying to accomplish is to rename alter the name of the file, while keeping some pieces (such as nw, w) intact and start the numbering from 0000.

我的原始文件名如下:

knifer dying nw  0001.png
knifer dying nw  0002.png
knifer dying nw  0003.png
knifer dying nw  0004.png
knifer dying nw  0005.png
knifer dying nw  0006.png
knifer dying nw  0007.png
knifer dying nw  0008.png
knifer dying nw  0009.png
knifer dying w  0001.png
knifer dying w  0002.png
knifer dying w  0003.png
knifer dying w  0004.png
knifer dying w  0005.png
knifer dying w  0006.png
knifer dying w  0007.png
knifer dying w  0008.png
knifer dying w  0009.png

我要完成的工作是使其看起来像这样:

What I want to accomplish is to make it look like this:

knifer_die_nw_0000.png <-- notice the _, the change of dying, keeping nw intact, and 0000
knifer_die_nw_0001.png
knifer_die_nw_0002.png
knifer_die_nw_0003.png
knifer_die_nw_0004.png
knifer_die_nw_0005.png
knifer_die_nw_0006.png
knifer_die_nw_0007.png
knifer_die_nw_0008.png
knifer_die_w_0000.png
knifer_die_w_0001.png
knifer_die_w_0002.png
knifer_die_w_0003.png
knifer_die_w_0004.png
knifer_die_w_0005.png
knifer_die_w_0006.png
knifer_die_w_0007.png
knifer_die_w_0008.png

我正在尝试编写一个使用sed来执行此操作的脚本.下面是我用来将0001转换为0000的命令.对于其他数字,我计划包括从0002到0001,从0003到0002的更改,依此类推.因此,通过将其变成10行bash脚本,我计划实现自己的目标.

I'm trying to write a script that does this by using sed. Below is the command I use to convert 0001 to 0000. For the other numbers I plan to include a change from 0002 to 0001, 0003 to 0002 and so forth. So, by turning this into a 10 line bash script I plan to achieve my target.

ls knifer*dying* | sed -E 's/knifer( |_)dying( |_)([a-z]{1,2})( ){1,2}0001(.*)/mv & knifer_die_\3_0000\5/'
mv knifer dying nw  0001.png knifer_die_nw_0000.png
knifer dying nw  0002.png
knifer dying nw  0003.png
knifer dying nw  0004.png
knifer dying nw  0005.png
knifer dying nw  0006.png
knifer dying nw  0007.png
knifer dying nw  0008.png
knifer dying nw  0009.png
mv knifer dying sw  0001.png knifer_die_sw_0000.png
knifer dying sw  0002.png
knifer dying sw  0003.png
knifer dying sw  0004.png
knifer dying sw  0005.png
knifer dying sw  0006.png
knifer dying sw  0007.png
knifer dying sw  0008.png
knifer dying sw  0009.png

我正在获取所需的输出.现在,如果我尝试使用 sh或bash 执行该问题,就会出现以下错误:

I'm getting the output I need. Now, the problem comes if I try to execute it with sh or bash I get the following error :

 ls knifer*dying* | sed -E 's/knifer( |_)dying( |_)([a-z]{1,2})( ){1,2}0001(.*)/mv & knifer_die_\3_0000\5/' | sh
usage: mv [-f | -i | -n] [-v] source target
       mv [-f | -i | -n] [-v] source ... directory
sh: line 2: knifer: command not found
sh: line 3: knifer: command not found
sh: line 4: knifer: command not found
sh: line 5: knifer: command not found
sh: line 6: knifer: command not found
sh: line 7: knifer: command not found
sh: line 8: knifer: command not found
sh: line 9: knifer: command not found
usage: mv [-f | -i | -n] [-v] source target
       mv [-f | -i | -n] [-v] source ... directory
sh: line 11: knifer: command not found
sh: line 12: knifer: command not found
sh: line 13: knifer: command not found
sh: line 14: knifer: command not found
sh: line 15: knifer: command not found
sh: line 16: knifer: command not found
sh: line 17: knifer: command not found
sh: line 18: knifer: command not found

有什么办法解决这个问题吗?我使用的是Mac OS X Mountain Lion(10.8.4).

Any idea how to fix this? I'm on Mac OS X Mountain Lion (10.8.4).

推荐答案

尝试一下:

find . -name 'knifer*dying*' -print | while read file; do
    new_name=$(echo "$file" | awk ' { printf "knifer_die_%s_%04d.png", $3, $4-1 } ')
    mv "$file" "$new_name"
done
    在文件名中带有空格的
  1. 迭代ls输出不是一个好主意. find可以更好地处理特殊文件名.

  1. iterating of ls output isn't a good idea with spaces in the file names. find handles special file names better.

由于您需要存储信息(数字或新名称),因此我建议使用while读取循环.

Since you need to store information (either the number or the new name), I recommend using a while read loop.

Awk用空格分隔字段:

Awk separates the fields by spaces:

  • $1:'knifer'
  • $2:垂死"
  • $3:"nw"左右
  • $4:0001.png
  • $1: 'knifer'
  • $2: 'dying'
  • $3: 'nw' or so
  • $4: 0001.png

$4-1尝试将其解析为一个数字并减去一个,因此".png"被截断了.然后只需组合printf

The $4-1 tries to use parse it as a number and subtract one, so the '.png' is cut off.Then just combine the values in printf

在移动之前,请通过将mv替换为echo

Before moving please do a test run by replacing mv with echo

echo "$file" "$new_name"

这篇关于尝试在Mac OS X上使用sed重命名文件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 07:32