工作,学习,生活,这里将会有一些记录. 备用域名:http://meisw.wdlinux.cn 注册 | 登陆

转行为列,特定行为记录分割符

file
 

#
1
2
3
#
4
5
#
6
7
8
9
#

输出为

1,2,3
4,5
6,7,8,9

 

[root@Mylinux tmp]# awk 'BEGIN{RS="#";OFS=","}NF=NF' file
1,2,3
4,5
6,7,8,9

 

 
不过严谨的用法还是用
awk 'BEGIN{RS="#";OFS=","}{NF=NF;print}' file
或awk 'BEGIN{RS="#";OFS=","}{$1=$1;print}' file

因为如果file中有空行或$1为空的行
awk 'BEGIN{RS="#";OFS=","}NF=NF' file
awk 'BEGIN{RS="#";OFS=","}$1=$1' file
的用法会丢失这些行

 

http://blog.chinaunix.net/u3/91453/showart_1818733.html

« 上一篇 | 下一篇 »

Trackbacks

点击获得Trackback地址,Encode: UTF-8

发表评论

评论内容 (必填):