Chinaunix

标题: sed处理奇偶行调换问题 [打印本页]

作者: nwpu_wl    时间: 2012-09-10 10:07
标题: sed处理奇偶行调换问题
假设文件内容为
1
2
3
4
5

仅仅使用sed处理文件后为
2
1
4
3
5

各位大神帮忙看看~不用awk哈,只使用sed
作者: 609854    时间: 2012-09-10 10:17
  1. 循环 读取文本
  2. do
  3. for 循环两次
  4. do
  5. sed '1!G;h;$!d'
  6. done
  7. done
复制代码

作者: nwpu_wl    时间: 2012-09-10 10:23
609854 发表于 2012-09-10 10:17


额。。哥们你的语句你自己跑过吗。。。。。
作者: yinyuemi    时间: 2012-09-10 10:32
sed '1~2{h;d};G'

作者: nwpu_wl    时间: 2012-09-10 10:39
回复 4# yinyuemi

楼上这个是啥意思?

   
作者: llh580110    时间: 2012-09-10 10:40
  1. sed -n 'h;n;G;p'
复制代码

作者: nwpu_wl    时间: 2012-09-10 10:48
我自己瞎琢磨 好像如下命令能大致满足要求 可是多了一个空行 各位是否有更好的命令
seq 10|sed 'x;$!n;{G;$p;x;d}'
作者: kk5234    时间: 2012-09-10 10:50
  1. seq 10|sed -n 'h;$!{n;G};p'
复制代码

作者: nwpu_wl    时间: 2012-09-10 10:55
回复 8# kk5234

楼上太帅了!


   
作者: zooyo    时间: 2012-09-10 11:14
提示: 作者被禁止或删除 内容自动屏蔽
作者: blackold    时间: 2012-09-10 11:17
  1. sed 'N;s/\(.*\)\n\(.*\)/\2\n\1/'
复制代码

作者: nwpu_wl    时间: 2012-09-10 11:27
回复 11# blackold

巧妙哈


   
作者: blackold    时间: 2012-09-10 11:34
回复 12# nwpu_wl


    基本用法啊。
作者: nwpu_wl    时间: 2012-09-10 11:47
zooyo 发表于 2012-09-10 11:14
奇数行,有空再想想.


1~2 这个是如何理解呢。
作者: nwpu_wl    时间: 2012-09-10 11:50
seq 10 |sed -n '$!x;$!{n;G;p;d};$p'

我感觉我的搞法总是比较麻烦呢。。。
作者: Shell_HAT    时间: 2012-09-11 07:23
回复 14# nwpu_wl


       first~step
              Match every step’th line starting with line first.  For example, ‘‘sed -n 1~2p’’ will print all the odd-numbered lines in the input stream,  and  the  address  2~5
              will match every fifth line, starting with the second.  first can be zero; in this case, sed operates as if it were equal to step.  (This is an extension.)

作者: Shell_HAT    时间: 2012-09-11 07:25
回复 15# nwpu_wl


你学的还是基础用法,不如他们那帮人钻研这些奇淫巧计的时间长。
作者: waker    时间: 2012-09-11 10:10
sed 'N;s/\(.*\)\n\(.*\)/\2\n\1/' urfile




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