免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12345下一页
最近访问板块 发新帖
查看: 6537 | 回复: 46
打印 上一主题 下一主题

请教 如何做大小写变换 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-12-15 13:03 |只看该作者 |倒序浏览
比如有个字符串变量 str="abc_def_hig1" 这个类型的,,想转成

str="Abc_Def_Hig" 这个类型的,怎么样才可以呢?谢谢。

论坛徽章:
7
荣誉版主
日期:2011-11-23 16:44:17子鼠
日期:2014-07-24 15:38:07狮子座
日期:2014-07-24 11:00:54巨蟹座
日期:2014-07-21 19:03:10双子座
日期:2014-05-22 12:00:09卯兔
日期:2014-05-08 19:43:17卯兔
日期:2014-08-22 13:39:09
2 [报告]
发表于 2005-12-15 13:19 |只看该作者

论坛徽章:
0
3 [报告]
发表于 2005-12-15 13:30 |只看该作者
原帖由 r2007 于 2005-12-15 13:19 发表
参看
http://bbs.chinaunix.net/viewthr ... 5%26filter%3Ddigest


  1. #!/bin/sed -f
  2. s/$/\naAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ/
  3. :a
  4. s/\b\([a-z]\)\(.*\n.*\)\1\(.\)/\3\2\1\3/                                       
  5. ta
  6. s/\n.*//
复制代码


贴过来学习,没看懂,有awk版本的么?函数 tolower只能转换一个词啊。。

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
4 [报告]
发表于 2005-12-15 13:37 |只看该作者
原帖由 大蚂蚁 于 2005-12-15 13:30 发表



  1. #!/bin/sed -f
  2. s/$/\naAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ/
  3. :a
  4. s/\b\([a-z]\)\(.*\n.*\)\1\(.\)/\3\2\1\3/                                       
  5. ta
  6. s/\n.*//
复制代码

...

http://bbs.chinaunix.net/viewthread.php?tid=476260&extra=page%3D1&page=13  见122楼我以前写的 改改就能用

[ 本帖最后由 寂寞烈火 于 2005-12-15 13:39 编辑 ]

论坛徽章:
7
荣誉版主
日期:2011-11-23 16:44:17子鼠
日期:2014-07-24 15:38:07狮子座
日期:2014-07-24 11:00:54巨蟹座
日期:2014-07-21 19:03:10双子座
日期:2014-05-22 12:00:09卯兔
日期:2014-05-08 19:43:17卯兔
日期:2014-08-22 13:39:09
5 [报告]
发表于 2005-12-15 13:37 |只看该作者
1996年一个seder提出的lookup tables技术
供参考
---------- Forwarded message ----------
Date: Tue, 19 Nov 1996 05:34:17 -0500 (EST)
From: Greg Ubben <gsu@romulus.ncsc.mil>
To: af137@freenet.toronto.on.ca
Subject: Part 1: Using lookup tables with s///

Fellow seders,

    Because the sort/delimit/number script I posted last week is so
complicated, it's going to take far more words than code to explain
it.  So I'm going to try to approach the explanation in at least three
parts.  I'll first go over a general technique used in both the sort
and the counter, then I'll explain how the counter works (since it is
simpler than the sort), then barring any unforseen pianos, I'll explain
the sort last.  My approach to explaining things can be rather lengthy
as I try to generalize a lot, and also go over the many alternative
ways that things could be done, so that you understand the trade-offs
and learn more than just this one silly problem.  Hopefully the depth
will be a good compromise for everyone.  If there's anything you have
any questions on or need more explanation on, you can e-mail me and
I'll elaborate on that in the next part.

    The first thing to note is that both the sort and the counter
algorithms use "lookup tables", just like the case-conversion method
which I described in one of the first newsletters.  Lookup tables
rely on using the powerful \( \) and \d (\1,\2, etc.) back-reference
operators in the s/// (substitute) command -- in particular, the fact
that you can use the \d later on in the same pattern itself to find
another instance of a previously matched string.

    Basically, you first append the lookup table to the pattern space.
Then you need some kind of * pattern between the \(key\) you're looking
up and the lookup table, to skip over the text in between.  You can
think of this * as the search operator.  Then once you've looked it up,
you usually want to get something back from the table, so you have
another \(\) next to the \d.  (You can equate this to looking something
up in an associative array in awk.)  Then you just put the things back
in the right order.  You may need additional \(\) to save additional
portions of the pattern space that you need to put back.  You can either
choose to put the lookup table back to be used again next time, or you
can delete it and add it back next time you do another lookup.

    Here's a short example to help tie the above together.  Say you
have a single digit in the pattern space and you want to convert that
to the alphabetic name of the digit using the table lookup method
(vs using 10 substitute commands).  For example, convert "5" to "five".
Here's one way to do it:

    #  append the lookup table
    s/$/0zero1one2two3three4four5five6six7seven8eight9nine/
    #  lookup the key (digit) and replace with the value
    s/\(.\).*\1\([^0-9]*\).*/\2/

    Take some time to unders

论坛徽章:
7
荣誉版主
日期:2011-11-23 16:44:17子鼠
日期:2014-07-24 15:38:07狮子座
日期:2014-07-24 11:00:54巨蟹座
日期:2014-07-21 19:03:10双子座
日期:2014-05-22 12:00:09卯兔
日期:2014-05-08 19:43:17卯兔
日期:2014-08-22 13:39:09
6 [报告]
发表于 2005-12-15 13:39 |只看该作者
原帖由 寂寞烈火 于 2005-12-15 13:37 发表

http://bbs.chinaunix.net/viewthr ... age%3D1&page=13  见122楼我以前写的

这个连接直接到达122楼^_^
http://bbs.chinaunix.net/viewthr ... ;page=13#pid4330470

论坛徽章:
8
摩羯座
日期:2014-11-26 18:59:452015亚冠之浦和红钻
日期:2015-06-23 19:10:532015亚冠之西悉尼流浪者
日期:2015-08-21 08:40:5815-16赛季CBA联赛之山东
日期:2016-01-31 18:25:0515-16赛季CBA联赛之四川
日期:2016-02-16 16:08:30程序设计版块每日发帖之星
日期:2016-06-29 06:20:002017金鸡报晓
日期:2017-01-10 15:19:5615-16赛季CBA联赛之佛山
日期:2017-02-27 20:41:19
7 [报告]
发表于 2005-12-15 13:46 |只看该作者
awk 'BEGIN{FS=OFS=""}{for(i=1;i<=NF;i++)if($(i-1)~/[^a-z]/)$i=toupper($i);print}'

论坛徽章:
0
8 [报告]
发表于 2005-12-15 13:47 |只看该作者
原帖由 寂寞烈火 于 2005-12-15 13:37 发表

http://bbs.chinaunix.net/viewthread.php?tid=476260&extra=page%3D1&page=13  见122楼我以前写的

推销,学习,学习


  1. awk '{gsub(/,/," & ")}{for(i=1;i<=NF;i++)if($i!~/^[^a-z]/)printf toupper(substr($i,0,1))substr($i,2,length($i))" ";else printf substr($i,0,1)toupper(substr($i,2,1))substr($i,3,length($i))" ";printf "\n"}' txt
复制代码


gsub(/,/," & "

这里没懂,别的都懂,sed我看到斜线就想睡觉 让斑竹失望了,
不过我会保留下来慢慢研究的  还是多谢斑竹

论坛徽章:
0
9 [报告]
发表于 2005-12-15 13:49 |只看该作者
原帖由 waker 于 2005-12-15 13:46 发表
awk 'BEGIN{FS=OFS=""}{for(i=1;i<=NF;i++)if($(i-1)~/[^a-z]/)$i=toupper($i);print}'

如果FS=OFS可以为空的话,这个好象比烈火的那个要好很多

不过刚刚才知道FS OFS可以为空

论坛徽章:
8
摩羯座
日期:2014-11-26 18:59:452015亚冠之浦和红钻
日期:2015-06-23 19:10:532015亚冠之西悉尼流浪者
日期:2015-08-21 08:40:5815-16赛季CBA联赛之山东
日期:2016-01-31 18:25:0515-16赛季CBA联赛之四川
日期:2016-02-16 16:08:30程序设计版块每日发帖之星
日期:2016-06-29 06:20:002017金鸡报晓
日期:2017-01-10 15:19:5615-16赛季CBA联赛之佛山
日期:2017-02-27 20:41:19
10 [报告]
发表于 2005-12-15 13:53 |只看该作者
gsub(/,/," & ")

相当于

s/,/ & /g

这个懂吧?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP