Chinaunix

标题: 对所有程序文件的字符都转换为小写的脚本,有一个问题,请大家帮忙。 [打印本页]

作者: 灿烂小猪    时间: 2013-05-29 13:11
标题: 对所有程序文件的字符都转换为小写的脚本,有一个问题,请大家帮忙。
脚本如下:
  1. #!/bin.bash

  2. find /cygdrive/d/max/ -type f -name *.html > list.txt

  3. for i in $(cat list.txt)
  4. do
  5. cat "$i" | tr '[A-Z]' '[a-z]' > "$i"
  6. echo $i
  7. done
复制代码
list.txt如下:
  1. /cygdrive/d/max/basicDo.html
  2. /cygdrive/d/max/bdzx/app/expepf_return.html
  3. /cygdrive/d/max/bdzx/applyep - 副本.html
复制代码
脚本执行到/cygdrive/d/max/bdzx/applyep - 副本.cfm这个文件就过不去了。原因是有空格,for又分出来了-和副本.html。请问大家,在for中怎样把/cygdrive/d/max/bdzx/applyep - 副本.html这一行变成一个变量呢?另外,转换所有程序文件大小写有没有其他办法?不吝赐教,谢谢了。
作者: rdcwayx    时间: 2013-05-29 13:33
  1. find /cygdrive/d/max/ -type f -name *.html > list.txt

  2. while read i
  3. do
  4. cat "${i}" | tr '[A-Z]' '[a-z]' > "${i}"
  5. echo ${i}
  6. done < list.txt
复制代码

作者: 灿烂小猪    时间: 2013-05-29 13:34
哇,谢谢了。我试试
作者: dn833    时间: 2013-05-29 13:38
  1. echo "ABcdEF"|sed 's/[A-Z]/\l&/g'
复制代码

作者: 灿烂小猪    时间: 2013-05-29 13:49
你好的,你的sed我没看明白,请指导一下吧。

回复 4# dn833


   
作者: lbseraph    时间: 2013-05-29 13:54
sed的那是正则了~不过挺好用
作者: 灿烂小猪    时间: 2013-05-29 13:59
本帖最后由 灿烂小猪 于 2013-05-29 14:00 编辑

回复 6# lbseraph


是的,sed s/[A-Z]/匹配大写字母\l&这一块没看明白。\l是把&(匹配到的大写字母)转为小写么?   
作者: lbseraph    时间: 2013-05-29 14:10
本帖最后由 lbseraph 于 2013-05-29 14:10 编辑
灿烂小猪 发表于 2013-05-29 13:59
回复 6# lbseraph


对的,而\u是把匹配到的小写转换成大写。

参考链接:
http://www.cnblogs.com/cbscan/articles/2277351.html
作者: 灿烂小猪    时间: 2013-05-29 14:51
晓得了,谢谢哈。
作者: dn833    时间: 2013-05-29 15:11
回复 5# 灿烂小猪


    把所有大写字母变成小写。就这一个l=lowercase
用info sed命令,第3.5章节s替换参数里有详细介绍
  1. 3.5 The `s' Command
  2. ===================

  3. The syntax of the `s' (as in substitute) command is
  4. `s/REGEXP/REPLACEMENT/FLAGS'.  The `/' characters may be uniformly
  5. replaced by any other single character within any given `s' command.
  6. The `/' character (or whatever other character is used in its stead)
  7. can appear in the REGEXP or REPLACEMENT only if it is preceded by a `\'
  8. character.

  9.    The `s' command is probably the most important in `sed' and has a
  10. lot of different options.  Its basic concept is simple: the `s' command
  11. attempts to match the pattern space against the supplied REGEXP; if the
  12. match is successful, then that portion of the pattern space which was
  13. matched is replaced with REPLACEMENT.

  14.    The REPLACEMENT can contain `\N' (N being a number from 1 to 9,
  15. inclusive) references, which refer to the portion of the match which is
  16. contained between the Nth `\(' and its matching `\)'.  Also, the
  17. REPLACEMENT can contain unescaped `&' characters which reference the
  18. whole matched portion of the pattern space.  Finally, as a GNU `sed'
  19. extension, you can include a special sequence made of a backslash and
  20. one of the letters `L', `l', `U', `u', or `E'.  The meaning is as
  21. follows:

  22. `\L'
  23.      Turn the replacement to lowercase until a `\U' or `\E' is found,

  24. `\l'
  25.      Turn the next character to lowercase,

  26. `\U'
  27.      Turn the replacement to uppercase until a `\L' or `\E' is found,

  28. `\u'
  29.      Turn the next character to uppercase,

  30. `\E'
  31.      Stop case conversion started by `\L' or `\U'.
复制代码

作者: seesea2517    时间: 2013-05-29 18:02
lz 的问题其实是文件名带空格用来 for 会出事嘛,所以针对性的改的话:

  1. [seesea@UC ~]$ cat file
  2. /cygdrive/d/max/basicDo.html
  3. /cygdrive/d/max/bdzx/app/expepf_return.html
  4. /cygdrive/d/max/bdzx/applyep - 副本.html

  5. [seesea@UC ~]$ for i in $(cat file); do echo $i; done;
  6. /cygdrive/d/max/basicDo.html
  7. /cygdrive/d/max/bdzx/app/expepf_return.html
  8. /cygdrive/d/max/bdzx/applyep
  9. -
  10. 副本.html

  11. [seesea@UC ~]$ IFS=\n'; for i in $(cat file); do echo $i; done;  
  12. /cygdrive/d/max/basicDo.html
  13. /cygdrive/d/max/bdzx/app/expepf_return.html
  14. /cygdrive/d/max/bdzx/applyep - 副本.html
复制代码

作者: 灿烂小猪    时间: 2013-05-29 22:42
回复 11# seesea2517

真太感谢了,你的办法也非常不错。




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2