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 02 03 04 05 06 07 08 09
    11 12 13 14 15 16 17 18 19

    2、实现将连续的三列数据合并为一列数据

    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
    11 12 13 14 15 16 17 18 19
    root@DESKTOP-1N42TVH:/home/test# for i in `head -n 1 a.txt | awk '{print NF}' | xargs seq`; do cut -d " " -f $i a.txt | paste -s -d " " >> temp1; done
    root@DESKTOP-1N42TVH:/home/test# ls
    a.txt  temp1
    root@DESKTOP-1N42TVH:/home/test# cat temp1  ## 转置后数据
    01 11
    02 12
    03 13
    04 14
    05 15
    06 16
    07 17
    08 18
    09 19
    root@DESKTOP-1N42TVH:/home/test# awk '{if(NR % 3 == 0) {print $0} else {printf("%s ", $0)}}' temp1 > temp2  ## 每三行合并为一行
    root@DESKTOP-1N42TVH:/home/test# ls
    a.txt  temp1  temp2
    root@DESKTOP-1N42TVH:/home/test# cat temp2
    01 11 02 12 03 13
    04 14 05 15 06 16
    07 17 08 18 09 19
    root@DESKTOP-1N42TVH:/home/test# for i in `head -n 1 temp2 | awk '{print NF}' | xargs seq`; do cut -d " " -f $i temp2 | paste -s -d " " >> result; done
    root@DESKTOP-1N42TVH:/home/test# ls
    a.txt  result  temp1  temp2
    root@DESKTOP-1N42TVH:/home/test# cat a.txt
    01 02 03 04 05 06 07 08 09
    11 12 13 14 15 16 17 18 19
    root@DESKTOP-1N42TVH:/home/test# cat result  ## 最终结果
    01 04 07
    11 14 17
    02 05 08
    12 15 18
    03 06 09
    13 16 19
  • 相关阅读:
    Orchard:如何生成模块和生成一个Content Part
    马云2011年邮件
    asp.net页面编码问题
    创建一个三D立体感的主页
    25个网页设计实例
    设计一个简洁的个人网站
    新浪微博产品交互改进[转]
    设计一个暗色调简洁漂亮的主页
    用HTML5 画LOGO
    成功企业站设计思路
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/15779444.html
Copyright © 2011-2022 走看看