2008年07月07日

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

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

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

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

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

2.
[cocobear@cocobear ~]$ ’s/\s\+//;s/\s\+\|$/|/g’ test.txt
AAB|BB|CCC|
A2B|CB|ABC|
3.
[cocobear@cocobear ~]$ ’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
#—————————–#

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

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

标签 :

3 楼了已经

  • dream写于08年07月07日

    sed ’s/ \+/|/g’ 中\+是什么意思?

  • cocobear写于08年07月07日

    表示匹配一个或者多个字符。

  • dream写于08年07月10日

    那 .* 不是也可以达到目的吗?

发表评论

在下面加入你的评论,或者 trackback 从你的博客站点。 订阅本文的评论。

:

:

:

«
»