删除whitspace,并将小写字母改为连字符-文件名 [英] Strip whitspace and make lower case up to hyphen - filename

查看:66
本文介绍了删除whitspace,并将小写字母改为连字符-文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多文本文件,我正在使用特定格式重命名.在下面,我能够删除空格并使小写.但是,这不是理想的结果.

I have a lot of text files which I am renaming with a specific format. Below I am able to remove whitespaces and make lower case. However, it is not the desired outcome.

我怎样才能格式化(删除空格并使其小写)连字符中的所有内容,然后仅在连字符之后占用第一个空白?

How would I be able to format(take out spaces and make lower case) everything up the hyphen and then take only the first white space after the hyphen?

find /temp/ -depth -name "* *" -type f -exec rename 's/ +\././; y/A-Z /a-z_/' {} +

输入结果:

Hello Video - KEEP.txt

输出:

hello_video_-_keep.txt

所需结果:

hello_video_-KEEP.txt

推荐答案

如果它是文件,我将使用:

If it was a file I would use:

sed -re 's/^([^-]*)-\s*([^\.]*)/\L\1-\U\2/' -e 's/ /_/g' file

  • s/^([^-]*)-\s*([^\.]*)/\L\1-\U\2/将从文件开头到连字符-的所有内容转换为小写形式.然后将其转换为大写字母,直到点为止.
  • s/ /_/g将所有空格转换为下划线_.
    • s/^([^-]*)-\s*([^\.]*)/\L\1-\U\2/ converts to lowercase everything from the beginning of the file up to a dash -. Then it converts to uppercase up to the dot.
    • s/ /_/g converts all spaces into underscores _.
    • 对于给定的文本,它返回:

      For your given text it returns:

      hello_video_-KEEP.txt
      


      如果要保留从-.的单词,请使用\E恢复大小写.然后,我们也可以通过将多余的_-替换为-(我知道这有点难看)来摆脱它.


      If you want to keep the word as it is from the - up to ., use \E to restore the case. Then, we can also get rid of the superfluous _- by replacing it to just - (a bit ugly, I know).

      $ cat file
      Hello Video - KEEP.txt
      My File - KeEp.txt
      $ sed -re 's/^([^-]*)-\s*([^\.]*)/\L\1\E-\2/' -e 's/ /_/g' -e 's/_-/-/g' file
      hello_video-KEEP.txt
      my_file-KeEp.txt
      

      提醒一下,这些是更改大小写的方法:

      As a reminder, these are the ways to change the upper/lower case:

      • \L-将所有后续字符转换为小写
      • \U-将所有后续字符转换为大写
      • \E-在当前情况下保留所有后续字符
      • \L - convert all proceeding characters to lowercase
      • \U - convert all proceeding characters to uppercase
      • \E - leave all proceeding characters in their current case

      您如何标记它有效?循环浏览find命令的结果:

      How can you mark it work? Loop through the results of the find command:

      while read -r file
      do
         new_file=$(echo "$file" | sed -re 's/^([^-]*)-\s*([^\.]*)/\L\1\E-\2/' -e 's/ /_/g' -e 's/_-/-/g')
         echo "mv '$file' '$new_file'"
      done < <(find . -type f ...)
      

      对于给定的输入,这将产生以下内容:

      For the given input this will produce this content:

      mv './My File - KeEp.txt' './my_file-KeEp.txt'
      mv './Hello Video - KEEP.txt' './hello_video-KEEP.txt'
      

      一旦确定它可以工作,只需删除echo并单独使用mv.注意引号是必须的!否则,它将无法正确处理文件名中的空格.

      once you are sure it works, just remove the echo and use mv alone. Note the quotes are necessary!! Otherwise it won't handle properly the spaces in the file names.

      这篇关于删除whitspace,并将小写字母改为连字符-文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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