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

问题描述

目标

更改这些文件名:


  • F00001-0708 RG-biasliuyda

  • F00001-0708-CS-akgdlaul

  • F00001-0708-VF-hioulgigl

这些文件名:


  • F0001-0708-RG-biasliuyda

  • F0001-0708-CS-akgdlaul

  • F0001-0708-VF-hioulgigl

壳牌code

要测试:

ls F00001-0708-*|sed 's/\(.\).\(.*\)/mv & \1\2/'

要执行:

ls F00001-0708-*|sed 's/\(.\).\(.*\)/mv & \1\2/' | sh

我的提问

我不明白的sed code。我明白了什么替代
命令

I don't understand the sed code. I understand what the substitutioncommand

$ sed 's/something/mv'

表示。我理解了正前pressions一些。但我不
明白这里发生了什么:

means. And I understand regular expressions somewhat. But I don'tunderstand what's happening here:

\(.\).\(.*\)

或此:

& \1\2/

前者,对我来说,只是看起来像它的意思是:单个字符,
其次是单个字符,后面的任何长度序列
单个字符 - 但肯定还有比这更给它至于。
后期:

The former, to me, just looks like it means: "a single character,followed by a single character, followed by any length sequence of asingle character"--but surely there's more to it than that. As far asthe latter part:

& \1\2/

我不知道。我真的想了解这个code。请帮帮我
在这里,伙计们。

I have no idea. I really want to understand this code. Please help meout here, guys.

推荐答案

首先,我应该说,要做到这一点最简单的方法是使用
preNAME或重命名命令。

First, I should say that the easiest way to do this is to use theprename or rename commands.

在Ubuntu中,OSX(家酿包改名,MacPorts的包 P5-文件重命名),或其他系统用Perl重命名(preNAME):

On Ubuntu, OSX (Homebrew package rename, MacPorts package p5-file-rename), or other systems with perl rename (prename):

rename s/0000/000/ F0000*

或从UTIL-Linux的NG,如RHEL重命名系统:

or on systems with rename from util-linux-ng, such as RHEL:

rename 0000 000 F0000*

这是很多比同等sed命令更容易理解。

That's a lot more understandable than the equivalent sed command.

但对于了解sed命令,sed的手册页是很有帮助的。如果
你运行男子sed和搜索&安培; (使用/命令搜索),
你会发现它在S /富/酒吧/替换特殊字符。

But as for understanding the sed command, the sed manpage is helpful. Ifyou run man sed and search for & (using the / command to search),you'll find it's a special character in s/foo/bar/ replacements.

  s/regexp/replacement/
         Attempt  to match regexp against the pattern space.  If success‐
         ful,  replace  that  portion  matched  with  replacement.    The
         replacement may contain the special character & to refer to that
         portion of the pattern space  which  matched,  and  the  special
         escapes  \1  through  \9  to refer to the corresponding matching
         sub-expressions in the regexp.

因此​​, \\(\\)匹配的第一个字符,可以通过 \\ 1引用
然后匹配下一个字符,这始终是0。
然后 \\(。* \\)文件名的其余部分,可以通过 \\引用匹配2

Therefore, \(.\) matches the first character, which can be referenced by \1.Then . matches the next character, which is always 0.Then \(.*\) matches the rest of the filename, which can be referenced by \2.

替换字符串把它一起使用&安培; (原
文件名)和 \\ 1 \\ 2 这是文件名的每一个部分,除了第二
性格,这是一个0。

The replacement string puts it all together using & (the originalfilename) and \1\2 which is every part of the filename except the 2ndcharacter, which was a 0.

这是要做到这一点,恕我直言,一个pretty神秘的方式。如果
某些原因,重命名命令不支持,你想使用
SED做重命名(或者你做的事情太复杂
对于改名?),是更加明确在你的正则表达式将使它更
更具可读性。也许是这样的:

This is a pretty cryptic way to do this, IMHO. If forsome reason the rename command was not available and you wanted to usesed to do the rename (or perhaps you were doing something too complexfor rename?), being more explicit in your regex would make it muchmore readable. Perhaps something like:

ls F00001-0708-*|sed 's/F0000\(.*\)/mv & F000\1/' | sh

能够看到什么在真正改变
S /搜索/置换/使它更具可读性。此外,它不会让
人物吸吮你的文件名,如果你不小心运行
两次或东西。

Being able to see what's actually changing in thes/search/replacement/ makes it much more readable. Also it won't keepsucking characters out of your filename if you accidentally run ittwice or something.

这篇关于使用sed的大规模重命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 05:43