os: Solaris 10
shell对字符串操作功能那是强,像sed,awk,tr之类的工具的子函数功能很多。比如:
但是,这些shell往往用起来不是很方便,他们对regexp正规表达式的支持总是limit的。我在处理单行的字符串时就遇到的麻烦,愣是看着手边的一堆shell工具拿那一行字符串没有了办法。
bash-3.00# projects -l default
default
projid : 3
comment: ""
users : (none)
groups : (none)
attribs: project.max-shm-memory=(priv,4294967295,deny)
现在我就是要读那个max-shm-memory的值4294967295。如果用perl正则,那很简单。但是shell处理多行好像有方法可行,就是单行里面的很困难。试了很久,想出一个偷梁换柱的方法。shell的awk处理有空格的单行字符可是行家。那我就造出来个。
先处理一下
bash-3.00# projects -l default | grep project.max-shm-memory
attribs: project.max-shm-memory=(priv,4294967295,deny)
bash-3.00# projects -l default | grep project.max-shm-memory | sed 's/attribs: //'
project.max-shm-memory=(priv,4294967295,deny)
造空格
bash-3.00# projects -l default | grep project.max-shm-memory | sed 's/attribs: //'| sed 's/,/ /g'
project.max-shm-memory=(priv 4294967295 deny)
读数,
bash-3.00# projects -l default | grep project.max-shm-memory | sed 's/attribs: //'| sed 's/,/ /g'| awk '{print $2}'
4294967295
bash-3.00#
搞定了。
shell的工具基本可以做你想要的,发挥你的想象力吧。