zoukankan      html  css  js  c++  java
  • linux系统中将指定数目行合并为一行

    1、创建测试数据

    利用awk进行合并(1)

    [root@linuxprobe test2]# seq -w 30 | xargs -n 2 | sed = | sed 'N;s/\n/> /' > a.txt ##创建测试数据
    [root@linuxprobe test2]# cat a.txt
    1> 01 02
    2> 03 04
    3> 05 06
    4> 07 08
    5> 09 10
    6> 11 12
    7> 13 14
    8> 15 16
    9> 17 18
    10> 19 20
    11> 21 22
    12> 23 24
    13> 25 26
    14> 27 28
    15> 29 30
    [root@linuxprobe test2]# awk '{if (NR%2==0){print $0} else {printf "%s ",$0}}' a.txt  ## 每两行合并为一行
    1> 01 02 2> 03 04
    3> 05 06 4> 07 08
    5> 09 10 6> 11 12
    7> 13 14 8> 15 16
    9> 17 18 10> 19 20
    11> 21 22 12> 23 24
    13> 25 26 14> 27 28
    15> 29 30 [root@linuxprobe test2]# awk '{if (NR%2==0){print $0} else {printf "%s ",$0}}' a.txt | sed '$ s/$/\n/' ## 没两行合并为一行
    1> 01 02 2> 03 04
    3> 05 06 4> 07 08
    5> 09 10 6> 11 12
    7> 13 14 8> 15 16
    9> 17 18 10> 19 20
    11> 21 22 12> 23 24
    13> 25 26 14> 27 28
    15> 29 30
    [root@linuxprobe test2]# awk '{if (NR%3==0){print $0} else {printf "%s ",$0}}' a.txt | sed '$ s/$/\n/'  ##三行合并
    1> 01 02 2> 03 04 3> 05 06
    4> 07 08 5> 09 10 6> 11 12
    7> 13 14 8> 15 16 9> 17 18
    10> 19 20 11> 21 22 12> 23 24
    13> 25 26 14> 27 28 15> 29 30
    
    [root@linuxprobe test2]# awk '{if (NR%4==0){print $0} else {printf "%s ",$0}}' a.txt | sed '$ s/$/\n/'  ##四行合并
    1> 01 02 2> 03 04 3> 05 06 4> 07 08 
    5> 09 10 6> 11 12 7> 13 14 8> 15 16
    9> 17 18 10> 19 20 11> 21 22 12> 23 24
    13> 25 26 14> 27 28 15> 29 30

    利用awk进行合并(2)

    [root@linuxprobe test2]# cat a.txt
    1> 01 02
    2> 03 04
    3> 05 06
    4> 07 08
    5> 09 10
    6> 11 12
    7> 13 14
    8> 15 16
    9> 17 18
    10> 19 20
    11> 21 22
    12> 23 24
    13> 25 26
    14> 27 28
    15> 29 30
    [root@linuxprobe test2]# awk '{if(NR%2!=0) ORS=" "; else ORS="\n";print}' a.txt  ## 两行合并
    1> 01 02 2> 03 04
    3> 05 06 4> 07 08
    5> 09 10 6> 11 12
    7> 13 14 8> 15 16
    9> 17 18 10> 19 20
    11> 21 22 12> 23 24
    13> 25 26 14> 27 28
    15> 29 30 [root@linuxprobe test2]# awk '{if(NR%2!=0) ORS=" "; else ORS="\n";print}' a.txt | sed '$ s/$/\n/'
    1> 01 02 2> 03 04
    3> 05 06 4> 07 08
    5> 09 10 6> 11 12
    7> 13 14 8> 15 16
    9> 17 18 10> 19 20
    11> 21 22 12> 23 24
    13> 25 26 14> 27 28
    15> 29 30
    [root@linuxprobe test2]# awk '{if(NR%3!=0) ORS=" "; else ORS="\n";print}' a.txt | sed '$ s/$/\n/' ## 末尾多了空行??
    1> 01 02 2> 03 04 3> 05 06
    4> 07 08 5> 09 10 6> 11 12
    7> 13 14 8> 15 16 9> 17 18
    10> 19 20 11> 21 22 12> 23 24
    13> 25 26 14> 27 28 15> 29 30
    
    [root@linuxprobe test2]# awk '{if(NR%4!=0) ORS=" "; else ORS="\n";print}' a.txt | sed '$ s/$/\n/'  ## 四行合并
    1> 01 02 2> 03 04 3> 05 06 4> 07 08
    5> 09 10 6> 11 12 7> 13 14 8> 15 16
    9> 17 18 10> 19 20 11> 21 22 12> 23 24
    13> 25 26 14> 27 28 15> 29 30

     参考:https://www.cnblogs.com/Ghost-bird/p/11591861.html

  • 相关阅读:
    只需5分钟就能Get到的神器:Python虚拟环境安装&使用
    Linux——28年桌面进化史
    Linux使用Pidstat命令查看进程状态信息
    提高思维能力的书籍推荐你看这本《决策必读12篇》
    管理和自我管理:领导者自我管理的重要性
    带团队看什么书 ?这本书教你提升团队凝聚力
    基于RNN和CTC的语音识别模型,探索语境偏移解决之道
    详解Spring中Bean的作用域与生命周期
    物联网通信技术,那些你不知道的事
    一招教你数据仓库如何高效批量导入与更新数据
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/13773550.html
Copyright © 2011-2022 走看看