可可熊的窝

shell脚本解题

IN:Shell, 编程相关   Tags: , ,    Comments:3

以下内容来自http://bbs.chinaunix.net/thread-1189933-1-1.html

现有数据,每一行中各个段间的分隔为多个空格,如下
  AAB BB CCC
A2B CB ABC
如何实现,转换成下面的格式
AAB|BB|CCC|
A2B|CB|ABC|

awk解法:
1.
[cocobear@cocobear ~]$ awk ‘BEGIN{OFS=”|”} {$1=$1;print}’ test.txt
AAB|BB|CCC
A2B|CB|ABC
结果最后少一个”|”
2.
[cocobear@cocobear ~]$ awk ‘{print $1″|”$2″|”$3″|”}’ test.txt
AAB|BB|CCC|
A2B|CB|ABC|

3.
[cocobear@cocobear ~]$ awk -v OFS=’|’ ‘NF++’ test.txt
AAB|BB|CCC|
A2B|CB|ABC|

sed解法:
1.
[cocobear@cocobear ~]$ sed ‘s/^ \+//;s/ \+/|/g;s/ *$/|/;’ test.txt
AAB|BB|CCC|
A2B|CB|ABC|

2.
[cocobear@cocobear ~]$ sed ‘s/\s\+//;s/\s\+\|$/|/g’ test.txt
AAB|BB|CCC|
A2B|CB|ABC|
3.
[cocobear@cocobear ~]$ sed ‘s/^[[:blank:]]\+//;s/[[:blank:]]\+\|$/|/g’ test.txt
AAB|BB|CCC|
A2B|CB|ABC|

如何把一行的第一个字母换成大写?

文件如下: test.txt
#—————————–#
This is line number 1

THIS IS Line Number 2

ThiS is Line Number THREE

this is line Number four
#—————————–#

解法:
sed ‘s/^\(.\)/\u\1/’
sed ‘s/[a-z]/\u&/’

& 指代替前面查找的关键字即[a-z] \1是指前面用\(..\)定义的第一个标签

07-07
2008

3 Comments for shell脚本解题

Leave a Comment

loading...