Chinaunix
标题:
对所有程序文件的字符都转换为小写的脚本,有一个问题,请大家帮忙。
[打印本页]
作者:
灿烂小猪
时间:
2013-05-29 13:11
标题:
对所有程序文件的字符都转换为小写的脚本,有一个问题,请大家帮忙。
脚本如下:
#!/bin.bash
find /cygdrive/d/max/ -type f -name *.html > list.txt
for i in $(cat list.txt)
do
cat "$i" | tr '[A-Z]' '[a-z]' > "$i"
echo $i
done
复制代码
list.txt如下:
/cygdrive/d/max/basicDo.html
/cygdrive/d/max/bdzx/app/expepf_return.html
/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
find /cygdrive/d/max/ -type f -name *.html > list.txt
while read i
do
cat "${i}" | tr '[A-Z]' '[a-z]' > "${i}"
echo ${i}
done < list.txt
复制代码
作者:
灿烂小猪
时间:
2013-05-29 13:34
哇,谢谢了。我试试
作者:
dn833
时间:
2013-05-29 13:38
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替换参数里有详细介绍
3.5 The `s' Command
===================
The syntax of the `s' (as in substitute) command is
`s/REGEXP/REPLACEMENT/FLAGS'. The `/' characters may be uniformly
replaced by any other single character within any given `s' command.
The `/' character (or whatever other character is used in its stead)
can appear in the REGEXP or REPLACEMENT only if it is preceded by a `\'
character.
The `s' command is probably the most important in `sed' and has a
lot of different options. Its basic concept is simple: the `s' command
attempts to match the pattern space against the supplied REGEXP; if the
match is successful, then that portion of the pattern space which was
matched is replaced with REPLACEMENT.
The REPLACEMENT can contain `\N' (N being a number from 1 to 9,
inclusive) references, which refer to the portion of the match which is
contained between the Nth `\(' and its matching `\)'. Also, the
REPLACEMENT can contain unescaped `&' characters which reference the
whole matched portion of the pattern space. Finally, as a GNU `sed'
extension, you can include a special sequence made of a backslash and
one of the letters `L', `l', `U', `u', or `E'. The meaning is as
follows:
`\L'
Turn the replacement to lowercase until a `\U' or `\E' is found,
`\l'
Turn the next character to lowercase,
`\U'
Turn the replacement to uppercase until a `\L' or `\E' is found,
`\u'
Turn the next character to uppercase,
`\E'
Stop case conversion started by `\L' or `\U'.
复制代码
作者:
seesea2517
时间:
2013-05-29 18:02
lz 的问题其实是文件名带空格用来 for 会出事嘛,所以针对性的改的话:
[seesea@UC ~]$ cat file
/cygdrive/d/max/basicDo.html
/cygdrive/d/max/bdzx/app/expepf_return.html
/cygdrive/d/max/bdzx/applyep - 副本.html
[seesea@UC ~]$ for i in $(cat file); do echo $i; done;
/cygdrive/d/max/basicDo.html
/cygdrive/d/max/bdzx/app/expepf_return.html
/cygdrive/d/max/bdzx/applyep
-
副本.html
[seesea@UC ~]$ IFS=\n'; for i in $(cat file); do echo $i; done;
/cygdrive/d/max/basicDo.html
/cygdrive/d/max/bdzx/app/expepf_return.html
/cygdrive/d/max/bdzx/applyep - 副本.html
复制代码
作者:
灿烂小猪
时间:
2013-05-29 22:42
回复
11#
seesea2517
真太感谢了,你的办法也非常不错。
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2