• linux 系统中把一列数据变为一行数据


    1、测试数据

    [root@centos79 test]# cat a.txt
    01
    02
    03
    04
    05
    06
    07
    08
    09
    10

    2、xargs

    [root@centos79 test]# cat a.txt
    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    [root@centos79 test]# cat a.txt | xargs
    01 02 03 04 05 06 07 08 09 10

    3、tr命令

    [root@centos79 test]# cat a.txt
    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    [root@centos79 test]# cat a.txt | tr "
    " " " | sed 's/.$/
    /'
    01 02 03 04 05 06 07 08 09 10

    4、awk命令

    [root@centos79 test]# cat a.txt
    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    [root@centos79 test]# awk '{ORS = " "}{print} END {printf "
    "}' a.txt
    01 02 03 04 05 06 07 08 09 10

    5、awk命令

    [root@centos79 test]# cat a.txt
    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    [root@centos79 test]# awk '{printf("%s ", $0)} END {printf "
    "}' a.txt
    01 02 03 04 05 06 07 08 09 10

    6、sed

    [root@centos79 test]# cat a.txt
    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    [root@centos79 test]# sed ':a; N; s/
    / /; ta' a.txt
    01 02 03 04 05 06 07 08 09 10

    验证:

    [root@centos79 test]# cat a.txt
    s e t
    g i k
    z n c
    d w u
    [root@centos79 test]# cat a.txt | xargs
    s e t g i k z n c d w u
    [root@centos79 test]# cat a.txt | tr "
    " " " | sed 's/.$/
    /'
    s e t g i k z n c d w u
    [root@centos79 test]# awk '{ORS = " "} {print} END {printf "
    "}' a.txt
    s e t g i k z n c d w u
    [root@centos79 test]# awk '{printf("%s ", $0)} END {printf "
    "}' a.txt
    s e t g i k z n c d w u
    [root@centos79 test]# sed ':a; N; s/
    / /; ta' a.txt
    s e t g i k z n c d w u
  • 相关阅读:
    PHP substr_compare() 函数
    Learn X in Y minutes
    类型和程序设计语言
    C++17尝鲜
    《Ensemble Methods: Foundations and Algorithms》
    周志华:“深”为什么重要,以及还有什么深的网络
    阿里毕玄:系统架构师如何做好系统设计?
    调用链监控:Dapper、Istio... etc.
    zz京东电商推荐系统实践
    zzPony.ai 的基础架构挑战与实践
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/15042236.html
Copyright © 2020-2023  润新知