Chinaunix

标题: 把偶数行的内容放到上一行后面 [打印本页]

作者: 共舞秋雨    时间: 2013-03-12 10:54
标题: 把偶数行的内容放到上一行后面
hi,我现在有个如下需求,不知道怎么弄,求帮助。。
文本内容格式:
2
92
94
184
186
276
....
....
总行数不确定,两行到n行,但肯定是偶数行,怎么把偶数行的数字放到上一行的后面,想要的结果是
2 92
94 184
196 276
.... ....
在线等

作者: yestreenstars    时间: 2013-03-12 10:54
  1. xargs -n2
复制代码
测试:
  1. [root@localhost ~]# cat i
  2. 2
  3. 92
  4. 94
  5. 184
  6. 186
  7. 276
  8. [root@localhost ~]# xargs -n2 < i
  9. 2 92
  10. 94 184
  11. 186 276
  12. [root@localhost ~]#
复制代码

作者: yestreenstars    时间: 2013-03-12 11:06
本帖最后由 yestreenstars 于 2013-03-12 11:12 编辑

sed:
  1. sed 'N;s/\n/ /'
复制代码
awk:
  1. awk '{s=s!=""?s" "$0:$0}!(NR%2){print s;s=""}'
复制代码
or
  1. awk 'NR%2{printf $0" ";next}1'
复制代码

作者: 共舞秋雨    时间: 2013-03-12 11:25
thx,我仔细看看这个命令,没用了。回复 2# yestreenstars


   
作者: todayhero    时间: 2013-03-12 11:28
大家的手真快
来个awk的
  1. awk 'ORS=NR%2?"\t":"\n"' urfile
复制代码

作者: yestreenstars    时间: 2013-03-12 11:32
回复 4# 共舞秋雨


    没用了?啥意思?
作者: yestreenstars    时间: 2013-03-12 11:33
回复 5# todayhero


    不错~学习~
作者: TasteOracle    时间: 2013-03-12 11:51
  1. awk '{T=$0;getline;print T" "$0}'  file
复制代码

作者: 共舞秋雨    时间: 2013-03-12 12:26
没用过,打错个字,sorry..
共舞秋雨 发表于 2013-03-12 11:25
thx,我仔细看看这个命令,没用了。回复 2# yestreenstars

作者: cao627    时间: 2013-03-12 20:18
回复 5# todayhero
学习


   
作者: gd7592280    时间: 2013-03-14 10:51
怎么能没有paste呢,试试paste - -




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2