zoukankan      html  css  js  c++  java
  • linux系统中将一列数据转换为若干列数据(列的顺序不变)

    1、测试数据

    [root@centos79 test]# cat a.txt
    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20

    2、生成中间文件,假设4行为一列

    [root@centos79 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@centos79 test]# awk '{if(NR % 4 == 0) {print $0} else {printf "%s ", $0}}' a.txt | tee tmp
    01 02 03 04
    05 06 07 08
    09 10 11 12
    13 14 15 16
    17 18 19 20
    [root@centos79 test]# cat tmp
    01 02 03 04
    05 06 07 08
    09 10 11 12
    13 14 15 16
    17 18 19 20

    3、对临时文件进行转置

    [root@centos79 test]# cat tmp
    01 02 03 04
    05 06 07 08
    09 10 11 12
    13 14 15 16
    17 18 19 20
    [root@centos79 test]# for i in $(seq $(head -n 1 tmp | awk '{print NF}')); do cut -d " " -f $i tmp | awk '{ORS = " "}{print $0} END {printf "
    "}' >> b.txt; done
    [root@centos79 test]# cat b.txt
    01 05 09 13 17
    02 06 10 14 18
    03 07 11 15 19
    04 08 12 16 20
    [root@centos79 test]# cat a.txt
    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
  • 相关阅读:
    科技小论文
    一线架构—细化架构
    python20.04.10
    软件架构总结
    信息领域热词分析
    架构即未来阅读笔记二
    构架即未来阅读笔记一
    科技小论文之软件质量
    Pre-architecture的需求
    软件质量
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/15059522.html
Copyright © 2011-2022 走看看