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

    1、测试数据

    root@DESKTOP-1N42TVH:/home/test# ls
    a.txt
    root@DESKTOP-1N42TVH:/home/test# cat a.txt
    01 11
    02 12
    03 13
    04 14
    05 15
    06 16
    07 17
    08 18
    09 19
    10 20

    2、将每两行数据合并为一行数据

    a、sed实现

    root@DESKTOP-1N42TVH:/home/test# ls
    a.txt
    root@DESKTOP-1N42TVH:/home/test# cat a.txt
    01 11
    02 12
    03 13
    04 14
    05 15
    06 16
    07 17
    08 18
    09 19
    10 20
    root@DESKTOP-1N42TVH:/home/test# sed 'N; s/\n/ /' a.txt   ## 将每两行中间的换行符替换为空格
    01 11 02 12
    03 13 04 14
    05 15 06 16
    07 17 08 18
    09 19 10 20

    b、awk实现

    root@DESKTOP-1N42TVH:/home/test# ls
    a.txt
    root@DESKTOP-1N42TVH:/home/test# cat a.txt
    01 11
    02 12
    03 13
    04 14
    05 15
    06 16
    07 17
    08 18
    09 19
    10 20
    root@DESKTOP-1N42TVH:/home/test# awk '{if(NR % 2 == 0) {print $0} else {printf("%s ", $0)}}' a.txt
    01 11 02 12
    03 13 04 14
    05 15 06 16
    07 17 08 18
    09 19 10 20

    c、paste实现

    root@DESKTOP-1N42TVH:/home/test# ls
    a.txt
    root@DESKTOP-1N42TVH:/home/test# cat a.txt
    01 02
    03 04
    05 06
    07 08
    09 10
    11 12
    13 14
    15 16
    17 18
    19 20
    root@DESKTOP-1N42TVH:/home/test# cat a.txt | paste - -
    01 02   03 04
    05 06   07 08
    09 10   11 12
    13 14   15 16
    17 18   19 20
    root@DESKTOP-1N42TVH:/home/test# cat a.txt | paste - - -d " "
    01 02 03 04
    05 06 07 08
    09 10 11 12
    13 14 15 16
    17 18 19 20

    3、将每三行数据合并为一行数据

    a、awk实现

    root@DESKTOP-1N42TVH:/home/test# ls
    a.txt
    root@DESKTOP-1N42TVH:/home/test# cat a.txt
    01 11
    02 12
    03 13
    04 14
    05 15
    06 16
    07 17
    08 18
    09 19
    10 20
    root@DESKTOP-1N42TVH:/home/test# awk '{if(NR % 3 == 0) {print $0} else {printf("%s ", $0)}}' a.txt  ## 不完整行最后缺少空格
    01 11 02 12 03 13
    04 14 05 15 06 16
    07 17 08 18 09 19
    10 20 root@DESKTOP-1N42TVH:/home/test# awk '{if(NR % 3 == 0) {print $0} else {printf("%s ", $0)}}' a.txt | sed '$ s/$/\n/'  ## sed添加空格
    01 11 02 12 03 13
    04 14 05 15 06 16
    07 17 08 18 09 19
    10 20
    root@DESKTOP-1N42TVH:/home/test# awk '{if(NR % 3 == 0) {print $0} else {printf("%s ", $0)}}' a.txt | sed '$ s/$/\n/'
    01 11 02 12 03 13
    04 14 05 15 06 16
    07 17 08 18 09 19
    10 20

    b、paste实现

    root@DESKTOP-1N42TVH:/home/test# ls
    a.txt
    root@DESKTOP-1N42TVH:/home/test# cat a.txt
    01 02
    03 04
    05 06
    07 08
    09 10
    11 12
    13 14
    15 16
    17 18
    19 20
    root@DESKTOP-1N42TVH:/home/test# cat a.txt | paste - - -    ## 每三行合并为一行
    01 02   03 04   05 06
    07 08   09 10   11 12
    13 14   15 16   17 18
    19 20
    root@DESKTOP-1N42TVH:/home/test# cat a.txt | paste - - - -d " "   ## 指定输出分割符
    01 02 03 04 05 06
    07 08 09 10 11 12
    13 14 15 16 17 18
    19 20

    4、每四行数据合并为一行数据

    a、awk实现

    root@DESKTOP-1N42TVH:/home/test# ls
    a.txt
    root@DESKTOP-1N42TVH:/home/test# cat a.txt
    01 11
    02 12
    03 13
    04 14
    05 15
    06 16
    07 17
    08 18
    09 19
    10 20
    root@DESKTOP-1N42TVH:/home/test# awk '{if(NR % 4 == 0) {print $0} else {printf("%s ", $0)}}' a.txt
    01 11 02 12 03 13 04 14
    05 15 06 16 07 17 08 18
    09 19 10 20 root@DESKTOP-1N42TVH:/home/test# awk '{if(NR % 4 == 0) {print $0} else {printf("%s ", $0)}}' a.txt | sed '$ s/$/\n/'
    01 11 02 12 03 13 04 14
    05 15 06 16 07 17 08 18
    09 19 10 20
    root@DESKTOP-1N42TVH:/home/test# awk '{if(NR % 4 == 0) {print $0} else {printf("%s ", $0)}}' a.txt | sed '$ s/$/\n/' 
    01 11 02 12 03 13 04 14
    05 15 06 16 07 17 08 18
    09 19 10 20

    b、paste 实现

    root@DESKTOP-1N42TVH:/home/test# ls
    a.txt
    root@DESKTOP-1N42TVH:/home/test# cat a.txt
    01 02
    03 04
    05 06
    07 08
    09 10
    11 12
    13 14
    15 16
    17 18
    19 20
    root@DESKTOP-1N42TVH:/home/test# cat a.txt | paste - - - -
    01 02   03 04   05 06   07 08
    09 10   11 12   13 14   15 16
    17 18   19 20
    root@DESKTOP-1N42TVH:/home/test# cat a.txt | paste - - - - -d " "   ## 将四行合并为一行
    01 02 03 04 05 06 07 08
    09 10 11 12 13 14 15 16
    17 18 19 20
  • 相关阅读:
    Hadoop--Map/Reduce实现多表链接
    map/reduce实现 排序
    Hadoop-Map/Reduce实现实现倒排索引
    虚拟机之仅主机模式(HostOnly)链接外网设置
    hadoop家族之mahout安装
    SQLserver中的常量与变量、判断循环语句
    sqlserver中的数据转换与子查询
    SQLserver中常用的函数及实例
    sqlserver的增删改查
    SQLserver数据库基础
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/15779410.html
Copyright © 2011-2022 走看看