• linux中将矩阵数据转换为一行数据


    1、测试数据

    root@PC1:/home/test# ls
    a.txt
    root@PC1:/home/test# cat a.txt  ## 测试数据
    i u k f
    2 3 8 7
    s j 9 4

    形式一、按行排列

    2、xargs实现

    root@PC1:/home/test# ls
    a.txt
    root@PC1:/home/test# cat a.txt
    i u k f
    2 3 8 7
    s j 9 4
    root@PC1:/home/test# cat a.txt | xargs
    i u k f 2 3 8 7 s j 9 4

    3、awk实现

    (1)、

    root@PC1:/home/test# ls
    a.txt
    root@PC1:/home/test# cat a.txt
    i u k f
    2 3 8 7
    s j 9 4
    root@PC1:/home/test# cat a.txt | awk '{printf("%s ", $0)} END {printf("\n")}'
    i u k f 2 3 8 7 s j 9 4

    (2)、

    root@PC1:/home/test# ls
    a.txt
    root@PC1:/home/test# cat a.txt
    i u k f
    2 3 8 7
    s j 9 4
    root@PC1:/home/test# cat a.txt | awk '{ORS = " "} {print $0} END {printf("\n")}'
    i u k f 2 3 8 7 s j 9 4

    (3)、

    root@PC1:/home/test# ls
    a.txt
    root@PC1:/home/test# cat a.txt
    i u k f
    2 3 8 7
    s j 9 4
    root@PC1:/home/test# cat a.txt | awk 'BEGIN{RS = EOF} {gsub("\n", " "); print}'
    i u k f 2 3 8 7 s j 9 4

    4、paste实现

    root@PC1:/home/test# ls
    a.txt
    root@PC1:/home/test# cat a.txt
    i u k f
    2 3 8 7
    s j 9 4
    root@PC1:/home/test# cat a.txt | paste -s
    i u k f 2 3 8 7 s j 9 4

    5、sed实现

    root@PC1:/home/test# ls
    a.txt
    root@PC1:/home/test# cat a.txt
    i u k f
    2 3 8 7
    s j 9 4
    root@PC1:/home/test# cat a.txt | sed ':a; N; s/\n/ /; ta'
    i u k f 2 3 8 7 s j 9 4

    6、tr实现

    root@PC1:/home/test# ls
    a.txt
    root@PC1:/home/test# cat a.txt
    i u k f
    2 3 8 7
    s j 9 4
    root@PC1:/home/test# cat a.txt | tr "\n" " " | sed 's/$/\n/'
    i u k f 2 3 8 7 s j 9 4

    R实现:

    (1)、

    > dir()
    [1] "a.txt"
    > dat <- read.table("a.txt", header = F)
    > dat
      V1 V2 V3 V4
    1  i  u  k  f
    2  2  3  8  7
    3  s  j  9  4
    > res <- data.frame("x")
    > dim(res)
    [1] 1 1
    > for (i in 1:nrow(dat)) {
    +   res <- cbind(res, dat[i,])
    + }
    > res <- res[,-1]
    > res
      V1 V2 V3 V4 V1.1 V2.1 V3.1 V4.1 V1.2 V2.2 V3.2 V4.2
    1  i  u  k  f    2    3    8    7    s    j    9    4

    (2)、

    > dir()
    [1] "a.txt"
    > dat <- read.table("a.txt", header = F)
    > dat
      V1 V2 V3 V4
    1  i  u  k  f
    2  2  3  8  7
    3  s  j  9  4
    > res <- as.matrix(dat)[1:(nrow(dat) * ncol(dat))]
    > res
     [1] "i" "2" "s" "u" "3" "j" "k" "8" "9" "f" "7" "4"
    > res <- t(as.data.frame(res))
    > res
        [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
    res "i"  "2"  "s"  "u"  "3"  "j"  "k"  "8"  "9"  "f"   "7"   "4"  

    形式二、按列排列

    shell实现

    root@PC1:/home/test# ls
    a.txt
    root@PC1:/home/test# cat a.txt
    i u k f
    2 3 8 7
    s j 9 4
    root@PC1:/home/test# for i in `seq $(head -n 1 a.txt | awk '{print NF}')`; do cut -d " " -f $i a.txt >> b.txt; done
    root@PC1:/home/test# cat b.txt
    i
    2
    s
    u
    3
    j
    k
    8
    9
    f
    7
    4
    root@PC1:/home/test# cat b.txt | paste -s -d " "
    i 2 s u 3 j k 8 9 f 7 4

    R实现:

    > dir()
    [1] "a.txt"
    > dat <- read.table("a.txt", header = F)
    > dat
      V1 V2 V3 V4
    1  i  u  k  f
    2  2  3  8  7
    3  s  j  9  4
    > res <- vector()
    > for (i in 1:ncol(dat)) {
    +   res <- c(res, dat[,i])
    + }
    > res
     [1] "i" "2" "s" "u" "3" "j" "k" "8" "9" "f" "7" "4"
    > res <- t(as.data.frame(res))
    > res
        [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
    res "i"  "2"  "s"  "u"  "3"  "j"  "k"  "8"  "9"  "f"   "7"   "4"  
  • 相关阅读:
    flask
    redis实战之事物和持久化
    vue 工程从window 到mac
    mysql5.7 group by
    Error creating bean with name 'org.apache.cxf.spring.boot.autoconfigure.CxfAutoConfiguration
    当我回过头
    springmvc 接收json类型的数据封装到map中
    linux 下 home 目录磁盘爆满,rm 后仍然不行
    springboot启动时的一个bug
    vue 使用webpack 打包 出现UnhandledPromiseRejectionWarning: Error: "dependency" is not a valid chunk sort mode at HtmlWebpackPlugin.sortEntryChunks
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/16042170.html
Copyright © 2020-2023  润新知