Chinaunix

标题: 怎样最简便的方法将文本的首行和第二行合并? [打印本页]

作者: iocg    时间: 2014-07-10 10:09
标题: 怎样最简便的方法将文本的首行和第二行合并?
我的方法是这样的
  1. awk 'NR==1{s=$0} NR==2{s=s$0;print s} NR>2' i
复制代码
不知道有没有更简便的方法,sed可以做到吗?



作者: WilliBhamlll    时间: 2014-07-10 10:10
  1. sed '1N;s/\n//'
复制代码

作者: iocg    时间: 2014-07-10 10:14
回复 2# WilliBhamlll


    还真可以。你们为什么那么厉害?


作者: LikeLx    时间: 2014-07-10 10:17
  1. awk 'NR==1{getline s;print $0" "s;next}1'
复制代码

作者: Kasiotao    时间: 2014-07-10 10:18
  1. sed -i '1{N;s/\n//}' testfile
复制代码

作者: 机智的小学生    时间: 2014-07-10 10:26
awk 'NR<2{printf $0;next;}1'  testfile
这个也应该可以
作者: iocg    时间: 2014-07-10 10:44
回复 6# 机智的小学生

    这样也行. {:3_182:}
  1. awk 'NR<2{printf $0}1' testfile
复制代码

作者: yestreenstars    时间: 2014-07-10 10:46
回复 7# iocg

这样做第一行会打印两遍的~
   
作者: 机智的小学生    时间: 2014-07-10 10:50
回复 7# iocg
awk 'NR<2{printf $0"\t";next}1' testfile
呵呵,这个应该可以了吧?


   
作者: reyleon    时间: 2014-07-10 10:50
你们为什么那么厉害?
作者: zerostudy    时间: 2014-07-10 11:02
学习一下sed的几个命令
  1. sed  '1{h;d};2{x;G;s/\n//}' file
复制代码

作者: iocg    时间: 2014-07-10 11:22
本帖最后由 iocg 于 2014-07-10 11:24 编辑

回复 8# yestreenstars


    确实会打印两遍!!!



作者: yestreenstars    时间: 2014-07-10 11:23
回复 12# iocg

AAA不是打印了两遍吗?
   
作者: wh6585306    时间: 2014-07-10 11:38
看不懂哦~
作者: wh6585306    时间: 2014-07-10 11:38
谁能解释下两行代码呢
作者: yinyuemi    时间: 2014-07-10 11:43
回复 1# iocg


    awk '(ORS=NR>1?RS:"") || 1'
作者: li0924    时间: 2014-07-10 15:00
  1. awk '{ORS=NR==1?FS:RS}1'
复制代码

作者: 用户名注册后不能更改    时间: 2014-07-10 15:11
  1. head -1 file | tr '\n' ' ';tail -n+2 file
复制代码





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