码迷,mamicode.com
首页 > 其他好文 > 详细

sed和awk等方法实现列转成行

时间:2015-08-19 21:07:41      阅读:339      评论:0      收藏:0      [点我收藏+]

标签:列转行

 之前整理了一部分grep、sed和awk的文章,当然只是很基础的东西,平时我遇到的问题会把它们整理到一起,然后对比分析,这样印象会更深刻。


[root@localhost ~]# cat file 

1

2

3

4

5

6

7

8

9

10

11


先把列转成行,写了5方法:

1.xargs实现

[root@localhost ~]# cat file |xargs

1 2 3 4 5 6 7 8 9 10 11


2.tr实现,需要echo换行

[root@localhost ~]# cat file |tr "\n" " " ;echo

1 2 3 4 5 6 7 8 9 10 11 


3.awk实现。主要是ORS的动态设置,以及”%”求余的巧妙使用

[root@localhost ~]# awk ‘{if(NR%11){ORS=" "}else{ORS="\n"};print;}‘ file 

1 2 3 4 5 6 7 8 9 10 11

[root@localhost ~]# awk ‘ORS=NR%11?" ":"\n"{print}‘ file

1 2 3 4 5 6 7 8 9 10 11


4.perl实现

[root@localhost ~]# cat file |perl -pe ‘s/\n/ /‘ ;echo

1 2 3 4 5 6 7 8 9 10 11


5.sed是用N实现,有几行就要写几个N,这里是10个N

[root@localhost ~]# cat file |sed ‘N;N;N;N;N;N;N;N;N;N;s/\n/ /g‘

1 2 3 4 5 6 7 8 9 10 11


本文出自 “卡卡西” 博客,请务必保留此出处http://whnba.blog.51cto.com/1215711/1686121

sed和awk等方法实现列转成行

标签:列转行

原文地址:http://whnba.blog.51cto.com/1215711/1686121

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!